C++ - CLASSES
C++ - CLASSES
CLASSES
Introduction
m
e
t
h
o
d
1
(
)
;
m
e
t
h
o
d
2
(
)
;
----------
methodN();
};
public:
void GetData();
void PutData();
};
This class has data members: Id, Name, Age and Salary, two member
functions: GetData() and PutData().
The private keyword makes data and functions private, by default the data
member and member functions are private.
Private data and functions can be accessed only from inside the same class.
The public keyword makes data and function public. Public data and functions
can be accessed out of the class.
Here, Id, Name, Age and Salary are private members where as GetData() and
PutData() are public members.
If we try to access private data from outside of the class, complier throws
error.
This feature in OOP is known as data hiding.
When class is defined, only the specification for the object is defined; no
objects.
Instantiation of object means, create an object of class to access
its members. Object is a variable of class type.
Class members are accessed using the dot operator (.) between class’s object
and class’s member name.
public:
void GetData();
void PutData();
};
int main()
{
Employee Emp;
}
class Syntax:
▪ There is no need to tell complier about the class the function belongs to
because the definition of member function is already in the class.
Program
#include<iostream>
Using namespace std;
#include<conio.h>
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData() //Defining GetData()
{
cout<<”Enter Employee Id:”;
cin>>Id;
cout<<”Enter Employee Name:”;
cin>>Name;
cout<<”Enter Employee Age:”;
cin>>Age;
cout<<”Enter Employee Salary:”;
cin>>Salary;
}
void PutData() //Defining PutData()
{
cout<<endl<<”Employee Id: ”<<Id<<endl;
cout<<endl<<”Employee Name: ”<<Name<<endl;
cout<<endl<<”Employee Age: ”<<Age<<endl;
cout<<endl<<”Employee Salary: ”<<Salary<<endl;
}
};
int main()
{
Employee E; //Creating Object
E.GetData(); //Calling GetData()
E.PutData(); //Calling
PutData()
getch();
return 0;
}
Output:
Enter Employee Id: 101
Enter Employee Name: ABC
Enter Employee Age: 22
Enter Employee Salary:20000
Employee Id:101
Employee Name: ABC
Employee Age: 22
Employee Salary: 20000
Syntax:
Body of function
}
Class_name: a program may contain more than one class and these classes
may have similar member functions. Class_name:: tells the complier which class
the function belongs to and the scope of member function is restricted to the
class_name.
Function_name: can be any valid C++ identifier.
Argument list: represents the type and number of value function will take,
values are sent by the calling statement.
Example
#include<iostream>
Using namespace std;
#include<conio.h>
class Employee
{
int Id;
char Name[25];
int Age;
long Salary;
public:
void GetData();
void PutData();
};
Employee Id:101
Employee Name: ABC
Employee Age: 22
Employee Salary: 20000
Constructors
Class A
{
int x;
public:
A(); //constructor
};
Constructor can be defined either inside the class definition or outside class
definition using class name and scope resolution:: operator.
Class A
{
int i; public:
A();//constructor declared
};
#include<iostream>
Using namespace std;
#include<conio.h>
Class Cube
{
int side;
public:
Cube()
{
Side=25;
}
int getSide()
{
return side;
}
};
int main()
{
Cube c;
cout<<” Value of side is ”<<c.getSide();
getch();
return 0;
}
Output: Value of side is 25
Program
#include<iostream>
Using namespace std;
#include<conio.h>
Class Cube
{
int side;
public:
Cube(int x)
{
Side=x;
}
int getSide()
{
return side;
}
};
int main()
{
Cube c1(10);
Cube c2(20);
Cube c3(30);
cout<<Value of c1 side is: ”<<c1.getSide()<<endl;
cout<<Value of c2 side is: ”<<c2.getSide()<<endl;
cout<<Value of c3 side is: ”<<c3.getSide()<<endl;
getch();
return 0;
}
3) Copy constructor – copy constructor is special type of constructor which takes
an object as argument, and is used to copy values of data members of one
object into other object. A copy constructor takes a parameter which is
reference to its own class type.
Syntax:
ClassName(ClassName &old_obj);
Program- show the usage of copy constructor
#include<conio.h>
#include<string.h>
Class Student
{
int Roll;
char Name[25];
float Marks;
public:
void Display()
{
Cout<<”Roll: “<<Roll<<endl;
Cout<<”Name: “<<Name<<endl;
Cout<<”Marks: “<<Marks<<endl;
}
};
int main()
{
Student S1(1,”sam”,85.65);’
StudentS2(S1); //statement 1
Getch();
Return();
}
Output:
Values in object S1
Roll: 1
Name: sam
Marks: 85.65
Values in object S2
Roll: 2
Name: matt
Marks: 90.50
Operator overloading
Every operator has their own functionality to work with built-in data types.
Class is user defined data type and complier doesn’t understand how to use
operators with user-defined data types.
The function for operator is declared by using the operator keyword followed
by the operator.
To overload an operator, a special operator function is defined inside the
class as:
Syntax:
Class className
{
……
Public
returnType operator symbol(arguments)
{
………..
}
……
};
#include<iostream>
Cout<<”Rectangle 2: “;
R2.Display();
R3=R1+R2;
Cout<<”Rectangle 3: “;
R3.Display();
Getch();
Return();
}
Output:
Rectangle1: Length: 2 Breadth: 5
Rectangle2: Length: 3 Breadth: 4
Rectangle3: Length: 5 Breadth: 9
Class Rectangle
{
int L,B;
public:
Rectangle() //Default Constructor
{
L=0;
B=0;
}
void operator++() //operator overloading
{
L=L+2;
B=B+2;
}
void Display()
{
Cout<<”Length: “<<L<<”Breadth: “<<B<<endl;
}
};
int main()
{
Rectangle R;
Cout<<”Length Breadth before increment “<<endl;
R.Display();
++R
Cout<<”Length Breadth after increment “<<endl;
R.Display();
Getch();
Return();
}
Output:
Length Breadth before increment
Length: 0 Breadth: 0
Length Breadth after increment
Length: 2 Breadth: 2
Friends of classes
Syntax:
Class class_name
{
………
Friend return_type
function_name(arguments);
……..
}
Program
#include<iostream>
Using namespace std;
#include<conio.h>
Class Distance
Private:
int meter;
public:
Distance():meter(0) { }
//friend function
d.meter+=5;
return d.meter;
}
int main()
{
Distance D;
Cout<<”Distance is:”<<addFive(D);
Getch();
Return 0;
}
OUTPUT:
Distance is:5
In the above program, friend function addFive() is declared inside Distance class.
So, the private data variable meter can be accessed from this function.
2.Friend class
▪ Like a friend function, a class can also be made a friend of another class using
keyword friend.
▪ When a class is made a friend class, all the member functions of that class
become friend functions.
▪ Syntax:
……..
Class B;
Class A
{
//class B is a friend class of class A
Friend class B;
…….
}
Class B
{
…….
}
▪ Program:
Show the usage of friend class
#include<iostream>
#include<conio.h>
Class Rectangle
int L,B;
public:
L=10;
B=20;
}
friend class Square; //friend class
};
Class Square
{
int S;
public:
square()
{
S=5;
}
void Display(Rectangle Rect)
{
Cout<<”Length:”<<Rect.L<<endl;
Cout<<”Breadth:”<< Rect.L<<endl;
Cout<<”Side:”<< S<<endl;
}
};
int main()
{
Rectangle R;
Square S;
S.Display(R);
Getch();
Return 0;
}
OUTPUT:
Length: 10
Breadth: 20
Side: 5
In the above example, we have created two classes Rectangle and Square. In
order to access the private and protected members or Rectangle class by
Square class, we have made Square class, a friend class of Rectangle class.
Now, Square class access all data member and member functions of Rectangle
class.
Aggregation
#include<iostream>
#include<conio.h>
Class Address
Public:
Char addressline[30],city[30],state[30];
Strcpy(this->addressline, addressline);
Strcpy(this->city, city);
Strcpy(this->state, state);
};
Class Employee
Private:
public:
int id
Char name[30];
Employee(int id,char name[],Address*address)
{
this->id=id;
strcpy(this->name,name);
this->address=address;
}
void Display()
{
Cout<<id<<” ”<<address->addressline<<” “;
Cout<<address->city<<” “<<address->state;
}
};
int main()
{
Address a1=Address(“Flat-104,Sec-2”,”Hyderabad”,”Telangana”);
Employee e1=Employee(102,”hash”,&a1);
E1.diaplay();
Getch();
Return 0;
}
OUTPUT: