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;
[Link]=2;
[Link]();
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
[Link] = 2.5;
[Link] = 2.5;
[Link] = 2.5;
[Link]=10.5;
[Link]=2.5;
[Link]=5.5;
cout<<"rect 2*******"<<endl;
cout<<"area = "<<[Link]()<<endl;
cout<<"vol = "<<[Link]() <<endl;
cout<<"rect 1*****\n";
// calculate and display the area and volume of the room
cout << "Area of Rect = " << [Link]() << endl;
cout << "Volume of Rect = " << [Link]() << 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>>[Link];
cout<<"breadth= ";
cin>>[Link];
cout<<"height= ";
cin>>[Link];
//default input
[Link]=5;
[Link]=5;
[Link]=1;
// calculate and display the area and volume of the room
cout << "Area of Rect = " << [Link]() << endl;
cout << "Volume of Rect = " << [Link]() << endl;
cout<<"******* rect2********"<<endl;
cout<<"area= "<<[Link]()<<endl;
cout<<"vol= "<<[Link]()<<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
[Link]();
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;
[Link]();
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;
[Link]();
[Link]();
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;
[Link](10, 20);
[Link](); //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
[Link]("Country");
fout<<"USA\n";
fout<<"UK\n";
fout<<"South Korea\n";
fout<<"India\n";
[Link]();
[Link]("Capital");
fout<<"Washington\n";
fout<<"London\n";
fout<<"Seoul\n";
fout<<"Delhi\n";
[Link]();
const int n=80;
char line[n];
ifstream fin; // reading from file so ifstream and fin
[Link]("Country");
cout<<"Contents of country file:\n";
while([Link]()==0)
[Link](line,n);
cout<<line<<endl;
Ms. Vinicia Dsouza
Part II
[Link]();
[Link]("Capital");
cout<<"Contents of capital file:\n";
while([Link]()==0)
[Link](line,n);
cout<<line<<endl;
[Link]();
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