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

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version