Part II
Classes and Objects
#include<iostream.h>
#include<conio.h>
class sqr
public:
int side;
double a,v;
int area()
a=side*side;
return a;
int vol()
v=4*side;
return v;
void display()
cout<<"area is "<<area()<<"\nvol is "<<vol()<<endl;
};
void main()
Ms. Vinicia Dsouza
Part II
clrscr();
sqr obj;
obj.side=2;
obj.display();
getch();
Q] Example with default input
#include <iostream.h>
#include <conio.h>
// create a class
class Rect {
//acess modifier/specifier
public:
//data members
double length;
double breadth;
double height;
//member function
double calculateArea() {
return length * breadth;
Ms. Vinicia Dsouza
Part II
double calculateVolume() {
return length * breadth * height;
};
void main() {
clrscr();
// create object of Room class
Rect rect1;
Rect rect2;
// assign values to data members
rect1.length = 2.5;
rect1.breadth = 2.5;
rect1.height = 2.5;
rect2.length=10.5;
rect2.breadth=2.5;
rect2.height=5.5;
cout<<"rect 2*******"<<endl;
cout<<"area = "<<rect2.calculateArea()<<endl;
cout<<"vol = "<<rect2.calculateVolume() <<endl;
cout<<"rect 1*****\n";
// calculate and display the area and volume of the room
cout << "Area of Rect = " << rect1.calculateArea() << endl;
cout << "Volume of Rect = " << rect1.calculateVolume() << endl;
Ms. Vinicia Dsouza
Part II
getch();
Q] Example with default input as well as taking input from user
#include <iostream.h>
#include <conio.h>
// create a class
class Rect {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
double calculateVolume() {
return length * breadth * height;
};
void main() {
clrscr();
Ms. Vinicia Dsouza
Part II
// create object of Rect class
Rect rect1;
Rect rect2;
// assign values to data members
//taking input from user
cout<<"Enter the values\n";
cout<<"length= ";
cin>>rect1.length;
cout<<"breadth= ";
cin>>rect1.breadth;
cout<<"height= ";
cin>>rect1.height;
//default input
rect2.length=5;
rect2.breadth=5;
rect2.height=1;
// calculate and display the area and volume of the room
cout << "Area of Rect = " << rect1.calculateArea() << endl;
cout << "Volume of Rect = " << rect1.calculateVolume() << endl;
cout<<"******* rect2********"<<endl;
cout<<"area= "<<rect2.calculateArea()<<endl;
cout<<"vol= "<<rect2.calculateVolume()<<endl;
Ms. Vinicia Dsouza
Part II
getch();
Constructors and Destructors
#include <iostream.h>
#include<conio.h>
class HelloWorld{
public:
//Constructor
HelloWorld(){
cout<<"Constructor is called"<<endl;
//Destructor
~HelloWorld(){
cout<<"Destructor is called"<<endl;
//Member function
void display(){
cout<<"Hello World!"<<endl;
};
void main(){
//Object created
HelloWorld obj;
Ms. Vinicia Dsouza
Part II
//Member function called
obj.display();
getch();
Friend Function
Q] Square of a number using friend function (If asked in exam explain friend function with
example show this example)
#include <iostream.h>
#include<conio.h>
class Cal {
private:
int a;
// friend function
friend int sqr(Cal);
};
// friend function definition
int sqr(Cal s) {
cout<<"Enter a value\n";
cin>>s.a;
//accessing private members from the friend function
Ms. Vinicia Dsouza
Part II
// s.a = 3;
return s.a*s.a;
void main() {
clrscr();
Cal c;
cout << "Square: " << sqr(c);
getch();
Operator Overloading
//syntax
/*class className {
... .. ...
public:
returnType operator symbol (arguments) {
... .. ...
... .. ...
};
*/
Write this example in exam if asked
Ms. Vinicia Dsouza
Part II
//program
Q] Overload ++ when used as prefix
#include <iostream.h>
#include<conio.h>
class Count {
private:
int value;
public:
// Constructor to initialize count to 5
Count() : value(5) {}
// Overload ++ when used as prefix
void operator ++ () {
++value;
void display() {
cout << "Count: " << value << endl;
};
Ms. Vinicia Dsouza
Part II
void main() {
clrscr();
Count count1;
// Call the "void operator ++ ()" function
++count1;
count1.display();
getch();
Inheritance
Q] Single Inheritance
// inheritance syntax
/*
class class_name //base class
public:
};
class class_name: access_specifier_of_baseclass baseclass_name
Ms. Vinicia Dsouza
Part II
};
*/
Write this example in exam if asked
//program
#include<iostream.h>
#include<conio.h>
class base //base class
public:
void showbase()
cout<<"this is base class\n" ;
};
class derived: public base //derived class
public:
void showderived()
cout<<"this is derived class\n";
showbase();
Ms. Vinicia Dsouza
Part II
};
void main()
clrscr();
base obj;
derived obj1;
obj.showbase();
obj1.showderived();
getch();
Virtual Function
#include <iostream.h>
#include<conio.h>
class base {
public:
virtual void print()
cout << "print base class" << endl;
void show()
Ms. Vinicia Dsouza
Part II
cout << "show base class" << endl;
};
class derived : public base {
public:
void print()
cout << "print derived class" << endl;
void show()
cout << "show derived class" << endl;
};
void main()
clrscr();
base* bptr;
derived d;
bptr = &d;
Ms. Vinicia Dsouza
Part II
// virtual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
getch();
Data Abstration and Data Encapsulation
#include <iostream.h>
#include<conio.h>
class Eg_encapsulation // data encapsulation
private: //data is encapsuled that means it is hidden
int a, b;
public:
// method to set values of
// private members
void set(int x, int y)
a = x;
Ms. Vinicia Dsouza
Part II
b = y;
void display()
cout<<"a = " <<a << endl;
cout<<"b = " << b << endl;
};
void main()
clrscr();
Eg_encapsulation obj;
obj.set(10, 20);
obj.display(); //data abstraction ( What’s happening inside the function is not known)
getch();
File Handling
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
Ms. Vinicia Dsouza
Part II
clrscr();
ofstream fout; //creating a file so ofstream and writing in file so fout
fout.open("Country");
fout<<"USA\n";
fout<<"UK\n";
fout<<"South Korea\n";
fout<<"India\n";
fout.close();
fout.open("Capital");
fout<<"Washington\n";
fout<<"London\n";
fout<<"Seoul\n";
fout<<"Delhi\n";
fout.close();
const int n=80;
char line[n];
ifstream fin; // reading from file so ifstream and fin
fin.open("Country");
cout<<"Contents of country file:\n";
while(fin.eof()==0)
fin.getline(line,n);
cout<<line<<endl;
Ms. Vinicia Dsouza
Part II
fin.close();
fin.open("Capital");
cout<<"Contents of capital file:\n";
while(fin.eof()==0)
fin.getline(line,n);
cout<<line<<endl;
fin.close();
getch();
Make your own header file
Steps:
1] Make Area.h file and Cir.h file (can make any name file but saved it as .h)
2] To access these files add then in include part
#include<iostream.h>
#include<conio.h>
#include "Area.h" // name of your header file
#include "Cir.h" // name of your header file
void main()
Ms. Vinicia Dsouza
Part II
clrscr();
int r;
cout<<"\nEnter the radius\n";
cin>>r;
cout<<"The area is: ";
Area(r); // accessing your header file…..just like sqrt(n) for math.h
cout<<"\nThe circumference is: ";
Cir(r); // accessing your header file…..just like sqrt(n) for math.h
getch();
Area.h
Create a file called Area.h( this will be your header file for the previous example don’t forget to
save the file as .h)
#include<stdio.h>
float Area(int r)
float area = 3.14 * r * r;
printf("%0.2f",area); //0.2f will give only 2 values after decimal
return 0; // here we are using return statement as we didn’t use void main and iostream.h
Cir.h
Create a file called Area.h( this will be your header file for the previous example don’t forget to
save the file as .h)
Ms. Vinicia Dsouza
Part II
#include<stdio.h>
float Circumference(int r)
float cir = 2 * 3.14 * r;
printf("%0.2f",cir); //0.2f will give only 2 values after decimal
return 0; // here we are using return statement as we didn’t use void main and iostream.h
Important Programs
1) Find area and circumference of a circle using classes.
2) Any simple program using classes.
Important concepts in theory
1) Object oriented programming-definition, advantages.
2) Object and classes- inside class/outside class, access specifiers, data members, member
functions.
3) Constructors and destructors- definition and example.
4) Friend functions- definition, example, advantages.
5) Polymorphism- types(compile time - operator overloading, function overloading, run
time- virtual function)
6) Type conversion- types
7) Inheritance- definition, types.
8) Virtual function- definition and rules (Any 8)
9) Operator overloading-definition and rules (Any 8)
Ms. Vinicia Dsouza
Part II
10) Data Abstraction- Definition
11) File handling- ifstream,ofstream,fstream,iostream,fin,fout
Ms. Vinicia Dsouza