TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    C++

    Define a C++ Class in Code::Blocks IDE

    Overview

    This tutorial outlines the steps to define a C++ class. We will use Code::Blocks IDE on Windows operating system.

    Define a C++ Class

    We will define an Employee class with two private member variables:

    • name
    • salary

    name member variable is the string data type. salary member variable is the double data type.

    // C++ class Employee
    class Employee
    {
    public:
    
     string getName()
     {
     return name; // return employees's name
     }
    
     double getSalary()
     {
     return salary; // return employees's name
     }
    
     void setName(string name)
     {
     this->name = name;// set employees's name
     }
    
     void setSalary(double salary)
     {
     this->salary = salary;// set employees's name
     }
    
    private:
     string name;
     double salary;
    };

     

    Define a C++ Class CodeBlocks

    We will now define the main routine. In the main, we will create an object of the class Employee. Set some values to the member variables: name and salary. We will set the values using the setter member functions.
    We will output the values by invoking the getter member functions.

     

    int main()
    {
     //Declare an Employee object
     Employee emp;
     //Invoke Setters
     emp.setName("John");
     emp.setSalary(2500.0);
     // Output
     cout << "Employee Name=" << emp.getName() << endl;
     cout << "Employee Salary=" << emp.getSalary()<< endl;
     return 0;
    } // end main

    Build and Run the C++ project. We should get the output of the object.

    Define C++ Class Output CodeBlocks

    That’s it.

    In the next tutorial, we will learn to the class in a separate header file with a *.h file extension.

    —

    C++ Tutorials

    C++ Tutorials on this website:

    https://www.testingdocs.com/c-coding-tutorials/

    For more information on the current ISO C++ standard

    https://isocpp.org/std/the-standard

    Related Posts

    g not found OpenSuse

    C++ /

    Install GCC C++ Compiler on OpenSuse

    sum of digits c program

    C++ /

    Sum of Digits of a Number C++ Program

    C++ Hello World Program

    C++ /

    C++ Hello World Program

    C++ /

    Object-Oriented Programming Language Examples

    C++ /

    Object-Oriented Programming Features

    ‹ C++ Basic Input/Output› Run C++ Program on Linux

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com