2. SYLLABUS
Class
Object
Constructor & Destructor
Class Modifiers(Private, Public & Protected)
Data Member
Member Function
Static Data Member
Static Member Function
Friend Function
4. Class
C++ is an object-oriented programming language.
Everything in C++ is associated with classes and objects, along with its
attributes and methods.
For example: in real life, a car is an object.
The car has attributes, such as weight and color, and methods, such as drive
and brake.
Attributes and methods are basically variables and functions that belongs to
the class.
These are often referred to as "class members".
A class is a user-defined data type that we can use in our program, and it
works as an object constructor, or a "blueprint" for creating objects.
5. Create a Class
To create a class, use the class keyword:
Example
Create a class called "MyClass":
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
6. Create an Object
In C++, an object is created from a class. We have already created the class named MyClass,
so now we can use this to create objects.
To create an object of MyClass, specify the class name, followed by the object name.
To access the class attributes (myNum and myString), use the dot syntax (.) on the object:
Example
Create an object called "myObj" and access the attributes:
class MyClass { // The class
public: // Access specifier
7. Cont….
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
9. Constructor & Destructor
A constructor is a member function of a class that has the same name as the
class name.
It helps to initialize the object of a class.
It can either accept the arguments or not.
It is used to allocate the memory to an object of the class.
It is called whenever an instance of the class is created.
It can be defined manually with arguments or without arguments.
There can be many constructors in a class.
It can be overloaded but it can not be inherited or virtual.
There is a concept of copy constructor which is used to initialize an object
from another object.
11. Destructor
Like a constructor, Destructor is also a member function of a class that has
the same name as the class name preceded by a tilde(~) operator.
It helps to deallocate the memory of an object.
It is called while the object of the class is freed or deleted.
In a class, there is always a single destructor without any parameters so it
can’t be overloaded.
It is always called in the reverse order of the constructor.
if a class is inherited by another class and both the classes have a destructor
then the destructor of the child class is called first, followed by the
destructor of the parent or base class.
13. Class Modifiers(Private, Public &
Protected)
In C++, there are three access specifiers:
public - members are accessible from outside the class
private - members cannot be accessed (or viewed) from outside the class
protected - members cannot be accessed from outside the class, however,
they can be accessed in inherited classes.
14. Example
#include <iostream>
using namespace std;
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
15. Cont….
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (x is public)
myObj.y = 50; // Not allowed (y is private)
return 0;
}
16. Data Member
Data members include members that are declared with any of the fundamental
types, as well as other types, including pointer, reference, array types, bit fields,
and user-defined types.
You can declare a data member the same way as a variable, except that explicit
initializers are not allowed inside the class definition.
However, a const static data member of integral or enumeration type may have an
explicit initializer.
If an array is declared as a nonstatic class member, you must specify all of the
dimensions of the array.
A class can have members that are of a class type or are pointers or references to
a class type. Members that are of a class type must be of a class type that has
been previously declared. An incomplete class type can be used in a member
declaration as long as the size of the class is not needed. For example, a member
can be declared that is a pointer to an incomplete class type.
17. Member Function
Member functions are operators and functions that are declared as members of a
class.
Member functions do not include operators and functions declared with the friend
specifier.
These are called friends of a class.
You can declare a member function as static; this is called a static member
function.
A member function that is not declared as static is called a nonstatic member
function.
class x { public: int add() // inline member function
add {return a+b+c;};
private: int a,b,c;
};
18. Static Data Member
Static data members are class members that are declared
using static keywords. A static member has certain special characteristics
which are as follows:
Only one copy of that member is created for the entire class and is shared by
all the objects of that class, no matter how many objects are created.
It is initialized before any object of this class is created, even before the
main starts.
It is visible only within the class, but its lifetime is the entire program.
Syntax:
static data_type data_member_name;
19. // C++ Program to demonstrate
// the working of static data member
#include <iostream>
using namespace std;
class A {
public:
A()
{
cout << "A's Constructor Called " <<
endl;
}
};
20. Cont….
class B {
static A a;
public:
B()
{
cout << "B's Constructor Called " <<
endl;
}
};
22. Static Member Function
Static Member Function in a class is the function that is declared as static
because of which function attains certain properties as defined below:
A static member function is independent of any object of the class.
A static member function can be called even if no objects of the class exist.
A static member function can also be accessed using the class name through
the scope resolution operator.
A static member function can access static data members and static member
functions inside or outside of the class.
Static member functions have a scope inside the class and cannot access the
current object pointer.
You can also use a static member function to determine how many objects of
the class have been created.
23. // C++ Program to show the working of
// static member functions
#include <iostream>
using namespace std;
class Box
{
private:
static int length;
static int breadth;
static int height;
public:
24. Cont….
static void print()
{
cout << "The value of the length is: " << length << endl;
cout << "The value of the breadth is: " << breadth << endl;
cout << "The value of the height is: " << height << endl;
}
};
25. Cont….
// initialize the static data members
int Box :: length = 10;
int Box :: breadth = 20;
int Box :: height = 30;
// Driver Code
int main()
{
Box b;
26. Cont….
cout << "Static member function is called through Object name: n" << endl;
b.print();
cout << "nStatic member function is called through Class name: n" <<
endl;
Box::print();
return 0;
}
27. Output
Static member function is called through Object name:
The value of the length is: 10
The value of the breadth is: 20
The value of the height is: 30
Static member function is called through Class name:
The value of the length is: 10
The value of the breadth is: 20
The value of the height is: 30
28. Friend Function
A friend class can access private and protected members of other classes in
which it is declared as a friend.
It is sometimes useful to allow a particular class to access private and
protected members of other classes.
For example, a LinkedList class may be allowed to access private members of
Node.
We can declare a friend class in C++ by using the friend keyword.
Syntax:
friend class class_name; // declared in the base class
29. Cont…..
If a function is defined as a friend function in C++, then the protected and
private data of a class can be accessed using the function.
By using the keyword friend compiler knows the given function is a friend
function.
For accessing the data, the declaration of a friend function should be done
inside the body of a class starting with the keyword friend.
30. Advantages of Friend Function in C++
The friend function allows the programmer to generate more efficient codes.
It allows the sharing of private class information by a non-member function.
It accesses the non-public members of a class easily.
It is widely used in cases when two or more classes contain the interrelated
members relative to other parts of the program.
It allows additional functionality that is not used by the class commonly.
31. Example
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
// friend function
friend int addFive(Distance);
public:
32. Cont….
Distance() : meter(0) {}
};
// friend function definition
int addFive(Distance d) {
//accessing private members from the friend function
d.meter += 5;
return d.meter;
}
int main() {
34. 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.
35. Constructor(Default Constructor ,
Parameterized Constructor and Copy
Constructor)
A constructor in C++ is a special member function with exact same name as
the class name.
The constructor name is the same as the Class Name. Reason: Compiler uses
this character to differentiate constructors from the other member functions
of the class.
A constructor must not declare a return type or void. Reason: As it’s
automatically called and generally used for initializing values.
They can be defined inside or outside the class definition.
Automatically calls when an object is created for the class.
It uses to construct that means to initialize all the data members (variables)
of the class.
36. Constructors types in C++
1. Default Constructor
A constructor with no arguments (or parameters) in the definition is a
default constructor.
It is the type of constructor in C++ usually used to initialize data members
(variables) with real values.
Note: The compiler automatically creates a default constructor without data
member (variables) or initialization if no constructor is explicitly declared.
37. Default Constructor
#include <iostream>
using namespace std;
//Class Name: Default_construct
class Default_construct
{
public:
int a, b;
// Default Constructor
Default_construct()
{
38. Cont….
a = 100;
b = 200;
}
};
int main()
{
Default_construct con; //Object created
cout << "Value of a: " << con.a;
cout<< "Value of b: " << con.b;
return 0;
}
39. 2. Parameterized Constructor
Unlike the Default constructor, It contains parameters (or arguments) in the constructor
definition and declaration.
More than one argument can also pass through a parameterized constructor.
#include <iostream>
using namespace std;
// class name: Rectangle
class Rectangle {
private:
double length;
double breadth;
public:
42. 3. Copy Constructor
A copy constructor is the third type among various types of constructors in
C++.
The member function initializes an object using another object of the same
class.
It helps to copy data from one object to another.
43. Example
#include <iostream>
using namespace std;
// class name: Rectangle
class Rectangle {
private:
double length;
double breadth;
public:
// parameterized constructor
Rectangle(double l, double b) {
length = l;
breadth = b;
}
44. Cont….
// copy constructor with a Rectangle object as parameter copies data of the obj parameter
Rectangle(Rectangle &obj) {
length = obj.length;
breadth = obj.breadth;
}
double calculateArea() {
return length * breadth;
}
};
45. Cont….
int main() {
// create objects to call constructors
Rectangle obj1(10,6);
Rectangle obj2 = obj1; //copy the content using object
//print areas of rectangles
cout << "Area of Rectangle 1: " << obj1.calculateArea();
cout << "Area of Rectangle 2: " << obj2.calculateArea();
return 0;
}