Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

UNIT 2:

Friend Class:
A friend class can access private and protected members of other classes in which it is declared as a
friend.

We can declare a friend class in C++ by using the friend keyword.

Syntax:

friend class class_name;


#include <iostream>
#include<string.h>
using namespace std;

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.

A friend function can be:


1. A global function
2. A member function of another class

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

1. Global Function as Friend Function


#include <iostream.h>
#include<string.h>
using namespace std;

class Fruit {
private:
string colour;
protected:
int Price;

public:
Fruit ()
{
colour = “red”;
Price= 99;
}

friend void friendFunc(Fruit & obj);


};

void friendFunc(Fruit & obj)


{
cout << "Private Variable:-colour= " << obj.colour << endl;
cout << "Protected Variable:-price= " << obj.price;
}

int main()
{
Fruit object1;
friendFunc(object1);
return 0;
}

Output:
Private Variable:-colour=red
Protected Variable:-price=99

2. Member Function of Another Class as Friend Function

#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&);
};

void anotherClass::memberFunction(Fruit & obj)


{
cout << "Private Variable:-colour= " << obj.colour << endl;
cout << "Protected Variable:-price= " << obj.price;
}

int main()
{
Fruit apple;
anotherClass object2;
object2.memberFunction(apple);

return 0;
}

OUTPUT:
Private Variable:-colour=red
Protected Variable:-price=99

🡪Static data members in C++

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;

Box(double l = 2.0, double b = 2.0, double h = 2.0) {


cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
objectCount++; // Increase every time object is createD

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

cout << "Total objects: " << Box::objectCount << endl;

return 0;
}

OUTPUT:
Constructor called.
Constructor called.
Total objects: 2

Static Function Members

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;

Box(double l = 2.0, double b = 2.0, double h = 2.0) {


cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
objectCount++;
}

double Volume() {
return length * breadth * height;
}
static int getCount() {
return objectCount;
}
};

// Initialize static member of class Box


int Box::objectCount = 0;

int main(void) {
cout << "Inital Stage Count: " << Box::getCount() << endl;

Box Box1(3.3, 1.2, 1.5);


Box Box2(8.5, 6.0, 2.0);

// Print total number of objects after creating object.


cout << "Final Stage Count: " << Box::getCount() << endl;

return 0;
}

OUTPUT:
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2

🡪Const member functions in C++


● The const member functions are the functions which are declared as constant in the program.

● The object called by these functions cannot be modified.


● Use: so that accidental changes to object are avoided.

● A const member function can be called by any type of object.

● Non-const functions can be called by non-const objects only.

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

void Line::setLength( double len ) {


length = len;
}

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>

using namespace std;


class Line {
public:
void setLength( double len );
double getLength( void );
Line(double len);

private:
double length;
};

Line::Line( double len) {


cout << "Object is being created, length = " << len << endl;
length = len;
}

void Line::setLength( double len ) {


length = len;
}

double Line::getLength( void ) {


return length;
}

int main() {
Line line(10.0);

cout << "Length of line : " << line.getLength() <<endl;

line.setLength(6.0);

cout << "Length of line : " << line.getLength() <<endl;

return 0;
}

OUTPUT:

Object is being created, length = 10


Length of line : 10
Length of line : 6

3. Copy Constructor in C++


The copy constructor in c++ is a constructor that creates an object by initialising it with a previously
created object of the same class.

// C++ program to demonstrate the working


// of a COPY CONSTRUCTOR
#include <iostream>
using namespace std;

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

cout << "p1.x = " << p1.getX()


<< ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX()
<< ", p2.y = " << p2.getY();
return 0;
}

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

function ends, the program ends, a delete variable is called etc.

● Destructors are different from normal member functions as they don’t take any

argument and don’t return anything.

● Also, destructors have the same name as their class and their name is

preceded by a tilde(~).

#include<iostream>

using namespace std;

class Demo {

private:

int num1, num2;

public:

Demo(int n1, int n2) {

cout<<"Inside Constructor"<<endl;

num1 = n1;

num2 = n2;

void display() {

cout<<"num1 = "<< num1 <<endl;

cout<<"num2 = "<< num2 <<endl;

~Demo() {

cout<<"Inside Destructor";

}};

int main() {

Demo obj1(10, 20);


obj1.display();

return 0;

Output
Inside Constructor
num1 = 10
num2 = 20
Inside Destructor

You might also like