C++ Unit 3
C++ Unit 3
SYLABUS
Class Declaration:
Class class_name
{
Private:
Variable declaration;
Function declaration;
Public:
Variable declaration;
Function declaration;
}
CLASS
The body of the class enclosed within braces and terminated by a
semicolon. The class body contains the declaration of variables and
functions. These functions and vasraibles are collectively called as class
members.
The class members that have been declared as private can be
accessed only from with in the class and public members can be
accessed from outside the class.
Note: by default, means access specifiers is not given, the class members
are considered as private.
CLASS
CLASS
CLASS
Eg: If we consider a dog, then its state is - name, breed, color, and the behavior is - barking,
wagging the tail, running.
Example:
public class Dog
{
String breed; // Data Member
int age; // Data Member
String color; // Data Member
void barking() // Member Function
{
}
void hungry() // Member Function
{
}
{
}
}
CLASS
Class function Definition:
It describes how the class functions are implemented. Methods
are functions that belongs to the class.
Example:
Class item
{
Int number;
Float cost;
Public:
Void getdata(int a, float b);
Void putdata(void)
{
Cout<<number<<”\n”;
Cout<<cost<<”\n”;
}
};
CLASS
Outside class definition:
Here Member functions are only declared inside the class and should be
defined ouside the class.
Syntax:
returnType class_name :: function_name ( argument declaration)
{
Function body
}
CLASS
Example:
void item :: getdata ( int a, float b)
{
Number = a;
Cost = b;
}
Example:
item x;
item x, y, z;
CLASS
Accessing class members:
We can access the class members ( data members and functions) using objects.
Syntax:
objectName.functionName(actual_ arguments)
example:
x.getdata(100,75.5);
x.putdata();
example:
class alpha
{
int x;
int y;
public:
int z;
};
alpha p;
p.x = 5; // error , x is private
p.z=10 // valid.
CLASS
CLASS
Static Data Member
A data member of a class can be qualified as static. A static
member variable has certain special characteristics. These are:
1. It is initialized to zero when the first object of its class is created, no
other intialization is permitted.
2. Only one copy of that member is created for the entire class and is
shared by all the objects or class, no matter how many objects are
created.
3. It is visible only within the class, but lifetime is the entire program.
return 0;
}
Passing Objects to Functions
Output:
value of A: 10
value of A: 20
value of A: 30
Passing Objects to Functions
Pass by reference:
In pass by reference, only a reference ( address of the object) to that
object (not the entire object) is passed to the function. Thus, the changes made
to the object within the function are also reflected in the actual object.
Example:
#include <iostream>
using namespace std;
class Demo
{
private:
int a;
public:
void set(int x)
{
a = x;
}
Passing Objects to Functions
void sum(Demo &ob1, Demo &ob2)
{
a = ob1.a + ob2.a;
}
void print()
{
cout<<"Value of A : "<<a<<endl;
}
};
Passing Objects to Functions
int main()
{
//object declarations
Demo d1;
Demo d2;
Demo d3;
return 0;
}
Passing Objects to Functions
Output:
value of A: 10
value of A: 20
value of A: 30
Friend Function
Friend function:
Eg:
#include<iostream.h>
#include<conio.h>
class sample
{
int a;
Public:
void get(), put();
void sample::put()
{
cout<<"\nThe value is ="<<a<<endl;
}
Friend Function
int main()
{
cout<<"\n\t\tSWAPPING OF VALUES";
Sample x,y;
x.get();
y.get();
cout<<"\nBefore swapping\n";
x.put();
y.put();
exchange(x,y);
cout<<"\nAfter swapping\n";
x.put();
y.put();
getch();
return 0;
}
Friend Function
Output:
SWAPPING OF VALUES
Enter an Integer value= 5
Enter an Integer value= 6
Before swapping
The value is = 5
The value is = 6
After swapping
The value is = 6
The value is = 5
Returning Objects
Returning Objects:
A function can receive the object as an argument and can also return the
object to the called function.
Example:
#include <iostream>
using namespace std;
class Student
{
public:
double marks1, marks2;
};
Student createStudent()
{
Student student;
student.marks1 = 96.5;
student.marks2 = 75.0;
Returning Objects
// print member variables of Student
cout << "Marks 1 = " << student.marks1 << endl;
cout << "Marks 2 = " << student.marks2 << endl;
return student;
}
int main()
{
Student student1;
// Call function
student1 = createStudent();
return 0;
}
Returning Objects
Output:
Mark1 = 96.5
Mark2 = 75.6
CONSTRUCTOR
CONSTRUCTOR
Constructor:
A constructor is a special type of member function of a
class which initializes objects of a class. In C++, Constructor is
automatically called when object (instance of class) is created. It is a
special member function of the class because it does not have any return
type.
Example:
#include <iostream>
class sample
{
public:
int a, b;
// Default Constructor
sample()
{
a = 10;
b = 20;
}
};
Types of Constructor
int main()
{
// Default constructor called automatically when the object is created
sample c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 0;
}
Output:
a: 10
b: 20
Types of Constructor
2. Parameterized Constructors:
It is possible to pass arguments to constructors. Typically, these arguments
help initialize an object when it is created.
// parameterized constructors
#include <iostream>
class Point
{
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX()
{
Types of Constructor
return x;
}
int getY()
{
return y;
}
};
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
return 0;
}
Output:
p1.x = 10, p1.y = 15
Types of Constructor
Uses of Parameterized constructor:
It is used to initialize the various data elements of different objects with
different values when they are created.
It is used to overload constructors.
3.Copy Constructor:
A copy constructor is a member function which initializes an object
using another object of the same class.
Example:
#include <iostream>
class sample
{
public:
int x;
sample(int a) // parameterized constructor.
{
x=a;
}
Types of Constructor
sample(sample &i) // copy constructor
{
x = i.x;
}
};
int main()
{
sample a1(20); // Calling the parameterized constructor.
sample a2(a1); // Calling the copy constructor.
cout<<a2.x;
return 0;
}
Output:
20
DESTRUCTOR
DESTRUCTOR
Destructor:
int main(void)
{
Employee e1; //creating an object of Employee
DESTRUCTOR
Employee e2; //creating an object of Employee
return 0;
}
Output:
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked