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 C++ Class Constructor

Overview

In this tutorial, we will learn to define a C++ class constructor. When we define a class we can define its constructors.

C++ Class Constructor

A constructor is a special member function that is invoked to create a new object of that class. The name of the constructor function is always the same as the name of the class. C++ constructor has no return type.

The main use of a constructor is to initialize the member data variables of the newly created C++ object. Like other member functions, the constructor can be overloaded. A C++ class may have multiple overloaded constructors with different parameters.

 

C++ Class Constructor

 

If we omit the constructor in the class, the C++ compiler will generate a default no-arg constructor. The constructor with no parameters is called the default or no-arg constructor. If we define constructor/(s) in the class definition, the compiler will not supply this default constructor.

The code in a constructor definition typically sets the data members of the new object to valid default data values or to the values of the parameters passed into the constructor.

Sample C++ Program

 

/************************************
* C++ Class Constructor
* Filename: employee.cpp
* C++ Class Constructor Demo Program
* C++ Tutorials - www.TestingDocs.com
**************************************/
#include
#include
using namespace std;

// C++ class Employee
class Employee
{
public:
 // Employee C++ Class Constructor
 Employee(string name,double salary)
 {
 this->name=name;
 this->salary=salary;
 }

 Employee()
 {
 this->name="Default";
 this->salary=0.0f;
 }

 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;
};

int main()
{
 //Declare an Employee objects
 Employee emp1;
 Employee emp2("Mark",3000);
 //Invoke Setters
 emp1.setName("John");
 emp1.setSalary(2500.0);

 // Output
 cout << "C++ Tutorials - www.TestingDocs.com" << endl;
 cout << "Employee 1 = " << emp1.getName() << " | "<< emp1.getSalary() << endl;
 cout << "Employee 2 = " << emp2.getName() << " | "<< emp2.getSalary()<< endl;

 return 0;
} // end main

Program Output

C++ Tutorials – www.TestingDocs.com
Employee 1 = John | 2500
Employee 2 = Mark | 3000

C++ Class Constructor Program Output

—

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

‹ Run C++ Program on Linux› C++ Program to Find Quadratic Equation Roots

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