0% found this document useful (0 votes)
4 views

Classes in Cpp

Uploaded by

202474292
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Classes in Cpp

Uploaded by

202474292
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Classes in C++ Eng.

Eman Beshr

C++ Object
In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc.

In other words, object is an entity that has state and behavior. Here, state means data and
behavior means functionality.

Object is a runtime entity, it is created at runtime.

C++ Class
In C++, class is a group of similar objects. It is a template from which objects are created. It can
have fields, methods, constructors etc.

C++ Object and Class Example


Classes in C++ Eng. Eman Beshr

C++ Class Example: Initialize and Display data through method


Classes in C++ Eng. Eman Beshr

C++ Class Example: Store and Display Employee Information

#include <iostream>
using namespace std;
class ScholarHat {
public: // Public access specifier
int x; // Public attribute

void display() { //// Public method


cout << "x = " << x << endl;
}
};

int main() {
ScholarHat myObj;
myObj.x = 70;
myObj.display();
return 0;
}
Classes in C++ Eng. Eman Beshr

C++ Constructor
In C++, constructor is a special method which is invoked automatically at the time of object
creation. It is used to initialize the data members of new object generally. The constructor in C++
has the same name as class or structure.

C++ Default Constructor


A constructor which has no argument is known as default constructor. It is invoked at the time of
creating object.

C++ Parameterized Constructor


A constructor which has parameters is called parameterized constructor. It is used to provide
different values to distinct objects.
Classes in C++ Eng. Eman Beshr

What distinguishes constructors from a typical member function?


1. Constructor's name is the same as the class's
2. Default There isn't an input argument for constructors. However, input arguments are available
for copy and parameterized constructors.
3. There is no return type for constructors.
4. An object's constructor is invoked automatically upon creation.
5. It must be shown in the classroom's open area.
6. The C++ compiler creates a default constructor for the object if a constructor is not specified
(expects any parameters and has an empty body).

What are the characteristics of a constructor?


1. The constructor has the same name as the class it belongs to.
2. Although it is possible, constructors are typically declared in the class's public section. However,
this is not a must.
3. Because constructors don't return values, they lack a return type.
Classes in C++ Eng. Eman Beshr

4. When we create a class object, the constructor is immediately invoked.


5. Overloaded constructors are possible.
6. Declaring a constructor virtual is not permitted.
7. One cannot inherit a constructor.
8. Constructor addresses cannot be referenced to.
9. When allocating memory, the constructor makes implicit calls to the new and delete operators.

C++ Destructor
A destructor works opposite to constructor; it destructs the objects of classes. It can be defined
only once in a class. Like constructors, it is invoked automatically.

A destructor is defined like constructor. It must have same name as class. But it is prefixed with
a tilde sign (~).

Note: C++ destructor cannot have parameters. Moreover, modifiers can't be applied on destructors.
C++ Constructor and Destructor Example

Let's see an example of constructor and destructor in C++ which is called automatically.
Classes in C++ Eng. Eman Beshr

You might also like