C++ Class - Unit 2
C++ Class - Unit 2
Friend Class:
A friend class can access private and protected members of other classes in which it is declared as a
friend.
Syntax:
class Vehicle {
private:
int plateNumber;
protected:
string State;
public:
Vehicle ()
{
plateNumber = 1001;
State = “TN”;
}
friend class Frnd;
};
Class Frnd {
public:
void display(Vehicle & t)
{
cout << "The value of Private Variable = "<< t. plateNumber << endl;
cout << "The value of Protected Variable = "<< t.State;
}
};
int main()
{
Vehicle v1;
Frnd fri;
fri.display(v1);
return 0;
}
OUTPUT:
The value of Private Variable = 1001
The value of Protected Variable = TN
Friend Function
● A friend function is a special function in C++ that in spite of not being a member function of a
class has the privilege to access the private and protected data of a class.
● is declared as a friend using the keyword “friend” inside the class.
● By declaring a function as a friend, all the access permissions are given to the function.
● The keyword “friend” is placed only in the function declaration of the friend function and not in
the function definition or call.
● A friend function can be declared in any section of the class i.e. public or private or protected.
They are the non-member functions that can access and manipulate the private and protected
members of the class for they are declared as friends.
Syntax:
friend return_type function_name (arguments); // for a global function
or
friend return_type class_name::function_name (arguments); // for a member function of another class
class Fruit {
private:
string colour;
protected:
int Price;
public:
Fruit ()
{
colour = “red”;
Price= 99;
}
int main()
{
Fruit object1;
friendFunc(object1);
return 0;
}
Output:
Private Variable:-colour=red
Protected Variable:-price=99
#include <iostream>
#include<string.h>
using namespace std;
class Fruit;
class anotherClass {
public:
void memberFunction(base& obj);
};
class Fruit {
private:
string colour;
protected:
int Price;
public:
Fruit ()
{
colour = “red”;
Price= 99;
}
friend void anotherClass::memberFunction(Fruit&);
};
int main()
{
Fruit apple;
anotherClass object2;
object2.memberFunction(apple);
return 0;
}
OUTPUT:
Private Variable:-colour=red
Protected Variable:-price=99
Static data members are class members that are declared using static keywords.
A static member has the following special characteristics:
● 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 being created, even before main starts.
● It is visible only within the class, but its lifetime is the entire program
● We can't put it in the class definition but it can be initialized outside the class as done in the
following example by redeclaring the static variable, using the scope resolution operator :: to
identify which class it belongs to.
● They must be explicitly defined outside the class using the scope resolution operator.
Syntax
static data_type data_member_name;
eg:
#include <iostream>
using namespace std;
class Box {
private:
double length, breadth, height;
public:
static int objectCount;
double Volume() {
return length * breadth * height;
}
};
int Box::objectCount = 0;
int main(void) {
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
return 0;
}
OUTPUT:
Constructor called.
Constructor called.
Total objects: 2
By declaring a member function as static, you make it independent of any particular object of the class.
A static member function can be called even if no objects of the class exist
the static functions are accessed using only the class name and the scope resolution operator ::.
A static member function can only access
1. static data member,
2. other static member functions
3. any other functions from outside the class.
#include <iostream>
using namespace std;
class Box {
private:
double length, breadth, height;
public:
static int objectCount;
double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}
};
int main(void) {
cout << "Inital Stage Count: " << Box::getCount() << endl;
return 0;
}
OUTPUT:
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2
syntax :
datatype function_name const();
Eg:
#include<iostream>
using namespace std;
class Demo {
int val;
public:
Demo(int x = 0) {
val = x;
}
int getValue() const {
return val;
}
};
int main() {
const Demo d(28);
Demo d1(8);
cout << "The value using object d : " << d.getValue();
cout << "\nThe value using object d1 : " << d1.getValue();
return 0;
}
Output
The value using object d : 28
The value using object d1 : 8
CONSTRUCTOR:
A class constructor is a special member function of a class that is executed whenever we create new
objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all.
Constructors can be very useful for setting initial values for certain member variables.
Types:
1. Default Constructor
2. Parameterized Constructor
3. Copy Constructor in C++
1. Default Constructor
A constructor to which no arguments are passed is called the Default constructor. It is also called a
constructor with no parameters.
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line();
private:
double length;
};
Line::Line() {
cout << "Object is being created by constructor" << endl;
}
double Line::getLength() {
return length;
}
int main() {
Line L1;
L1.setLength(6.0);
cout << "Length of line : " << L1.getLength() <<endl;
return 0;
}
Output:
Object is being created by constructor
Length of line : 6
2) Parameterized Constructor
● A default constructor does not have any parameter, but if you need, a constructor can have
parameters.
● This helps you to assign initial value to an object at the time of its creation
#include <iostream>
private:
double length;
};
int main() {
Line line(10.0);
line.setLength(6.0);
return 0;
}
OUTPUT:
class Point {
private:
int x, y;
public:
Point(int x1, int y1)
{
x = x1;
y = y1;
}
// Copy constructor
Point(const Point& p1)
{
x = p1.x;
y = p1.y;
}
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
Destructors:
● Destructors in C++ are member functions in a class that delete an object.
● They are called when the class object goes out of scope such as when the
● Destructors are different from normal member functions as they don’t take any
● Also, destructors have the same name as their class and their name is
preceded by a tilde(~).
#include<iostream>
class Demo {
private:
public:
cout<<"Inside Constructor"<<endl;
num1 = n1;
num2 = n2;
void display() {
~Demo() {
cout<<"Inside Destructor";
}};
int main() {
return 0;
Output
Inside Constructor
num1 = 10
num2 = 20
Inside Destructor