0% found this document useful (0 votes)
8 views12 pages

C++ Notes

The document provides an overview of classes and objects in C++, explaining their definitions, characteristics, and differences. It covers key concepts such as member functions, constructors, destructors, and the use of the 'this' pointer. Additionally, it includes code examples to illustrate the implementation of these concepts in C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

C++ Notes

The document provides an overview of classes and objects in C++, explaining their definitions, characteristics, and differences. It covers key concepts such as member functions, constructors, destructors, and the use of the 'this' pointer. Additionally, it includes code examples to illustrate the implementation of these concepts in C++ programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Class:

A class in C++ is the building block that leads to Object-Oriented programming. It is


a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class. A
C++ class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of
them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class
and wheels, speed limits, mileage are their properties.

A Class is a user defined data-type which has data members and member
functions.
Data members are the data variables and member functions are the functions used
to manipulate these variables and together these data members and member
functions defines the properties and behavior of the objects in a Class.

Object:

An Object is an instance of a Class. When a class is defined, no memory is


allocated but when it is instantiated (i.e. an object is created) memory is allocated.

Class definition:
A class is defined in C++ using keyword class followed by the name of class. The
body of class is defined inside the curly brackets and terminated by a semicolon at
the end.

Declaring Objects: When a class is defined, only the specification for the object is
defined; no memory or storage is allocated. To use the data and access functions
defined in the class, you need to create objects.
Syntax:
ClassName ObjectName;

Accessing data members and member functions: The data members and
member functions of class can be accessed using the dot(‘.’) operator with the
object.
For example if the name of object is obj and you want to access the member
function with the name printName() then you will have to write obj.printName() .

Accessing Data Members


The public data members are also accessed in the same way given however the
private data members are not allowed to be accessed directly by the object.
Accessing a data member depends solely on the access control of that data
member. This access control is given by access modifiers in C++. There are three
access modifiers : public, private and protected.

// C++ program to demonstrate accessing of data members

#include <iostream>

using namespace std;

class Sample

// access specifier

public:

// Data Members

String name;

// Member Functions()

void printname()

cout << "name is:" << name;


}

};

int main()

// Declare an object of class Sample

Sample ob;

// accessing data member

ob.name = "Gayatri";

// accessing member function

ob.printname();

return 0;

Output
name is:Gayatri

Member Functions in Classes:

There are 2 ways to define a member function:


 Inside class definition
 Outside class definition

Inside class definition:


Member functions defined inside the class definition are by default inline, but you
can also make any non-class function inline by using keyword inline with them.
Inline functions are actual functions, which are copied everywhere during
compilation, like pre-processor macro, so the overhead of function calling is
reduced.

Outside class definition:


To define a member function outside the class definition we have to use the scope
resolution :: operator along with class name and function name.

/ C++ program to demonstrate function

// declaration outside class

#include <iostream>

using namespace std;

class Employee

public:

string name;

int id;

// printname is not defined inside class definition

void printname();

// printid is defined inside class definition

void printid()

cout <<"Employee id is: "<<id;

};

// Definition of printname using scope resolution operator ::


void Employee::printname()

cout <<"Employee name is: "<<name;

int main()

Employee ob;

ob.name = "Rahul";

ob.id=15

// call printname()

ob.printname();

// call printid()

ob.printid();

return 0;

Output:
Employee name is: Rahul

Employee id is: 15

Difference between Class and Object:

There are many differences between object and class. Some differences between
object and class are given below:
Class Object

Class is used as a template for declaring and An object is an instance of a


creating the objects. class.

When a class is created, no memory is Objects are allocated memory


allocated. space whenever they are created.

The class has to be declared first and only An object is created many times
once. as per requirement.

A class cannot be manipulated as they are not


available in the memory. Objects can be manipulated.

A class is a logical entity. An object is a physical entity.

It is created with a class name in


C++ and
It is declared with the class keyword with the new keywords in Java.

Each object has its own values,


Class does not contain any values which which are
can be associated with the field. associated with it.

A class is used to bind data as well as methods Objects are like a variable of the
together as a single unit. class.

Class Scope:

A name declared within a member function hides a declaration of the same name
whose scope extends to or past the end of the member function's class.

When the scope of a declaration extends to or past the end of a class definition, the
regions defined by the member definitions of that class are included in the scope of
the class. Members defined lexically outside of the class are also in this scope. In
addition, the scope of the declaration includes any portion of the declarator following
the identifier in the member definitions.

The name of a class member has class scope and can only be used in the following
cases:

 In a member function of that class

 In a member function of a class derived from that class


 After the . (dot) operator applied to an instance of that class
 After the . (dot) operator applied to an instance of a class derived from that
class, as long as the derived class does not hide the name
 After the -> (arrow) operator applied to a pointer to an instance of that class
 After the -> (arrow) operator applied to a pointer to an instance of a class
derived from that class, as long as the derived class does not hide the name
 After the :: (scope resolution) operator applied to the name of a class
 After the :: (scope resolution) operator applied to a class derived from that
class

Constructor:
Constructor in C++ is a special method that is invoked automatically at the time of
object creation. It is used to initialize the data members of new objects generally.
The constructor in C++ has the same name as the class and may be defined inside
or outside the class definition.
Characteristics of the constructor:
 The name of the constructor is the same as its class name.
 Constructors are mostly declared in the public section of the class though it can
be declared in the private section of the class.
 Constructors do not return values; hence they do not have a return type.
 A constructor gets called automatically when we create the object of the class.
 Constructors can be overloaded.
 Constructor can not be declared virtual.
 Constructor cannot be inherited.
 Addresses of Constructor cannot be referred.
 Constructor make implicit calls to new and delete operators during memory
allocation.

There are 3 types of constructors:


 Default constructors
 Parameterized constructors
 Copy constructors

// C++ program to demonstrate constructors

#include<iostream>

using namespace std;

class Demo_Constructor

{
public:

int id;

//Default Constructor

Def_const()

cout << "Default Constructor called" << endl;

id=-1;

//Parameterized Constructor

Par_const(int x)

cout <<"Parameterized Constructor called "<< endl;

id=x;

};

int main() {

// obj1 will call Default Constructor

Def_const obj1;

cout <<"id is: "<<obj1.id << endl;

// obj2 will call Parameterized Constructor


Par_const obj2(21);

cout <<"id is: " <<obj2.id << endl;

return 0;

Output:
Default Constructor called
id is: -1
Parameterized Constructor called
id is: 21

Copy Constructors is a type of constructor which is used to create a copy of an


already existing object of a class type. It is usually of the form X (X&), where X is
the class name. The compiler provides a default Copy Constructor to all the
classes.

Syntax of Copy Constructor


Classname(const classname & objectname)
{
....
}
As it is used to create an object, hence it is called a constructor. And, it creates a
new object, which is exact copy of the existing copy, hence it is called copy
constructor.

copy construction of objects

Below is a sample program on Copy Constructor:

#include<iostream>
using namespace std;
class Samplecopyconstructor
{
private:
int x, y; //data members

public:
Samplecopyconstructor(int x1, int y1)
{
x = x1;
y = y1;
}
/* Copy constructor */
Samplecopyconstructor (const Samplecopyconstructor &sam)
{
x = sam.x;
y = sam.y;
}

void display()
{
cout<<x<<" "<<y<<endl;
}
};
/* main function */
int main()
{
Samplecopyconstructor obj1(10, 15); // Normal constructor
Samplecopyconstructor obj2 = obj1; // Copy constructor
cout<<"Normal constructor : ";
obj1.display();
cout<<"Copy constructor : ";
obj2.display();
return 0;
}
Normal constructor : 10 15
Copy constructor : 10 15

Destructors:

Destructor is another special member function that is called by the compiler when
the scope of the object ends.
Destructor is a special member function like a constructor.
Destructors destroy the class objects that are created by
constructors.

The destructor have the same name as their class, preceded by a
~.

The destructor neither requires any arguments nor returns any
values.

It is automatic ally executed when the object goes out of score.

Destructor releases memory space occupied by the objects
Destructor is a special member function like a constructor. Destructors destroy the
class objects that are created by constructors. The destructor have the same name
as their class, preceded by a (~)tilde symbol. The destructor neither requires any
arguments nor returns any values. It is automatic ally executed when the object
goes out of score. Destructor releases memory space occupied by the objects
Characteristics of Destructors
 Their name is the same as the class name but is preceded by a tilde (~).
 They do not have return types, not even void and they cannot return values.
 Only one destructor can be defined in the class.
 Destructor neither has default values nor can be overloaded.
 We cannot refer to their addresses.
 An object with a constructor or destructor cannot be used as a member of a
union.
 They make “implicit calls‟ to the operators new and delete when memory
allocation/ memory de-allocation is required.
Example:
class Test
{
public:
Test()
{
}
cout<<“\n Constructor Called”;
~Test()
{
cout<<“\n Destructor Called”;
}
};
void main()
{
Test t;
}
Output:
Constructor Called
Destructor Called

This Pointer:

In C++ programming, this is a keyword that refers to the current instance of the
class. There can be 3 main usage of this keyword in C++.

o It can be used to pass current object as a parameter to another method.


o It can be used to refer current class instance variable.
o It can be used to declare indexers.

//Example of this keyword in C++ that refers to the fields of current class

#include <iostream>

using namespace std;

class Employee
{
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
Employee (int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main()
{
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee
e1.display();
e2.display();
return 0;
}
Output:
101 Sonoo 890000
102 Nakul 59000

You might also like