Sunteți pe pagina 1din 34

1.

Classes and Objects:


A. Write a simple C++ program to define a student class. Define its
member functions to read and display data of student.
Ans: I] Defining member functions inside the class:
#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
char name[10];
char course[10];
public:
void getdata()
{
cout<<"\n Enter your rollno, name, course=";
cin>>rollno>>name>>course;
}
void putdata()
{
cout<<"\n Rollno of student="<<rollno;
cout<<"\n Name of student="<<name;
cout<<"\n Course of student="<<course;

}
};
void main()
{
student s1;
clrscr();
s1.getdata();
s1.putdata();
getch();
}

II] Defining member functions inside the class:


#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
char name[10];
char course[10];
public:
void getdata();
void putdata();

};
void student::getdata()
{
cout<<"\n Enter your rollno, name, course=";
cin>>rollno>>name>>course;
}
void student::putdata()
{
cout<<"\n Rollno of student="<<rollno;
cout<<"\n Name of student="<<name;
cout<<"\n Course of student="<<course;

}
void main()
{
student s1;
clrscr();
s1.getdata();
s1.putdata();
getch();
}

B. Write a C++ program to define a class employee. Read and display the
data of 5 employees.
#include<iostream.h>
#include<conio.h>

class employee
{
int emp_num;
char dept[20];
char emp_name[15];
float sal;
public:

void get_details();
void show_emp_details();
};

void employee :: get_details()


{
cout<<"\nEnter employee number:\n";
cin>>emp_num;
cout<<"Enter employee name:\n";
cin>>emp_name;
cout<<"Enter employee's department:\n";
cin>>dept;
cout<<"Enter employee's Salary\n";
cin>>sal;
}

void employee :: show_emp_details()


{
cout<<"\nDetails of : "<<emp_name;
cout<<"\nEmployee number : "<<emp_num;
cout<<"\nDepartment : "<<dept;
cout<<"\nSalary : "<<sal;
}

int main()
{
employee emp[5];
int i,num;
clrscr();
for(i=0;i<5;i++)
emp[i].get_details();

for(i=0;i<5;i++)
emp[i].show_emp_details();

getch();
return 0;
}
2 Friend Function:
A. Write a C++ program to implement the friend function.

#include<iostream.h>
#include<conio.h>
class complex
{
int real,imag;
public:
void set()
{
cout<<"enter real and imag part";
cin>>real>>imag;
}
friend complex sum(complex,complex);
void display();
};
void complex::display()
{
cout<<"the sum of complex num is"<<real<<"+i"<<imag;
}
complex sum(complex c1,complex c2)
{
complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return c3;
}
int main()
{
complex a,b,c;
clrscr();
a.set();
b.set();
c=sum(a,b);
c.display();
getch();
return(0);
}

B. Design a class Static Demo to show the implementation of static


variable and static function.
#include<iostream.h>
#include<conio.h>
class test
{
int code;
static int count;
public :
void setcode()
{
code= ++count;
}

void showcode()
{
cout<<"object number"<<code;
}
static void showcount()
{
cout<<"count"<<count;
}
};
int test::count;

int main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
return(0);
}
3 Constructor and Destructor:
A. Design a class Complex for adding two complex numbers and
also show the use of constructor.
#include<iostream.h>
#include<conio.h>
class complex
{
int real,imag;
public:
complex()
{
real=0;
imag=0;
}
complex(int x, int y)
{
real=x;
imag=y;
}

friend complex sum(complex,complex);


void display()
{
cout<<"\n The complex num is"<<real<<"+i"<<imag;
}
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.real=c1.real+c2.real;
c3.imag=c1.imag+c2.imag;
return c3;
}
int main()
{
complex c1(2,3);
complex c2(3,4);
complex c3;
clrscr();
c1.display();
c2.display();
c3=sum(c1,c2);
cout<<"\n Sum of two complex number is=";
c3.display();
getch();
return(0);
}

B. Write a C++ program to implement destructor.


#include<iostream.h>
#include<conio.h>
class A
{
// constructor
A()
{
cout << "Constructor called";
}

// destructor
~A()
{
cout << "Destructor called";
}
};

void main()
{
A obj1; // Constructor Called
int x = 1
if(x)
{
A obj2; // Constructor Called
} // Destructor Called for obj2
} // Destructor called for obj1
4 Polymorphism:
A. Design a class Geometry containing the methods area() and
volume() and also overload the area() function .
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14

class fnover {
public:
void area(int); //circle
void area(int, int); //rectangle
void area(float, int, int); //triangle
};

void fnover::area(int a) {

cout << "\nArea of Circle:" << pi * a*a;


}

void fnover::area(int a, int b) {


cout << "\nArea of rectangle:" << a*b;
}

void fnover::area(float t, int a, int b)


{
cout << "\nArea of triangle:" << t * a*b;
}
void main()
{
fnover obj;
int r,a,b;
clrscr();
cout << "\n Enter Radious of the Circle:";
cin>>r;
obj.area(r);
cout << "\n Enter two Sides of the Rectangle:";
cin >> a>>b;
obj.area(a, b);
cout << "\n Enter two Sides of the Triangle:";
cin >> a>>b;
obj.area(0.5, a, b);
getch();
}

B. Overload the operator unary (-) for demonstrating


operator overloading.
I] Using normal member operator function:
#include<iostream.h>
#include<conio.h>
class abc
{
int a,b,c;
public:
void get()
{
cout<<"\nEnter three numbers: ";
cin>>a>>b>>c;
}
void show()
{
cout<<"\n\nA= "<<a<<"\tB= "<<b<<"\tC= "<<c;
}
void operator -()
{
a= -a;
b= -b;
c= -c;
}
}
void main()
{
clrscr();
abc a1;
a1.get();
cout<<"\n\n Original contents";
a1.show();
-a1;
cout<<"\n\n After Negation";
a1.show();
getch();
}

II] Using Friend function:


#include<iostream.h>
#include<conio.h>
class abc
{
int a,b,c;
public:
void get()
{
cout<<"\nEnter three numbers: ";
cin>>a>>b>>c;
}
void show()
{
cout<<"\n\nA= "<<a<<"\tB= "<<b<<"\tC= "<<c;
}
friend void operator -(abc &ob);
};
void operator -(abc &ob)
{
ob.a= -ob.a;
ob.b= -ob.b;
ob.c= -ob.c;
}

void main()
{
clrscr();
abc a1;
a1.get();
cout<<"\n\n Original contents";
a1.show();
-a1;
cout<<"\n\n After Negation";
a1.show();
getch();
}

C. Overload the operator binary (+) for demonstrating


operator overloading.
I] Using Friend function:
#include<iostream.h>
#include<conio.h>
class complex
{
int real,imag;
public:
void set()
{
cout<<"enter real&imag";
cin>>real>>imag;
}
friend complex operator+(complex,complex);
void display()
{
cout<<"the sum is"<<real<<"+i"<<imag;
}
};
complex operator+(complex t1,complex t2)

{
complex temp;
temp.real=t1.real+t2.real;
temp.imag=t1.imag+t2.imag;
return(temp);
}
int main()
{
complex t1,t2;
t1.set();
t2.set();
t1=t1+t2;
t1.display();
return(0);
}

II] Using normal member operator function:


#include<iostream.h>
#include<conio.h>
class complex
{
int real,imag;
public:
void set()
{
cout<<"enter real&imag";
cin>>real>>imag;
}
complex operator+(complex c);
void display()
{
cout<<"the sum is"<<real<<"+i"<<imag;
}
};
complex complex::operator+(complex c)
{
complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return(temp);
}
int main()
{
complex t1,t2;
t1.set();
t2.set();
t1=t1+t2;
t1.display();
return(0);
}
5 String Handling:
A. Write a C++ program for different string operations.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char str[60];
public:
void get()
{
cout<<"\n\tEnter a string: ";
cin>>str;
}
void show()
{
cout<<"\n"<<str;
}
string operator +(string s2)
{
string s3;
strcpy(s3.str,str);
strcat(s3.str,s2.str);
return s3;
}
};
void main()
{
clrscr();
string s1,s2,s3;
s1.get();
s2.get();
cout<<"\nString 1";
s1.show();
cout<<"\nString 2";
s2.show();
cout<<"\nAfter concatenation String 3";
s3=s1 + s2;
s3.show();
getch();
}
6 Inheritance:
A. Write a simple C++ program for single level
Inheritance.
I] By using public derivation:
#include<iostream.h>
#include<conio.h>
class base
{
private: int a;
public: int b;
void set_ab()
{
a=6;
b=4;
}
int geta()
{
return a;
}
void showa()
{
cout<<"\n a="<<a;
}
};
class derived:public base
{
private: int c;
public:
void mult()
{
c=b*geta();
}
void show()
{
cout<<"\n b="<<b;
cout<<"\n mlutiplication of a and b="<<c;
}
};
void main()
{
derived d1;
clrscr();
d1.set_ab();
d1.mult();
d1.showa();
d1.show();
getch();
}

II] By using private derivation:


#include<iostream.h>
#include<conio.h>
class base
{
private: int a;
public: int b;
void set_ab()
{
a=6;
b=4;
}
int geta()
{
return a;
}
void showa()
{
cout<<"\n a="<<a;
}
};
class derived:private base
{
private: int c;
public:
void mult()
{
set_ab();
c=b*geta();
}
void show()
{
showa();
cout<<"\n b="<<b;
cout<<"\n mlutiplication of a and b="<<c;
}
};
void main()
{
derived d1;
clrscr();
// d1.set_ab(); Error
d1.mult();
// d1.showa(); Error
d1.show();
getch();
}

B. Write a simple C++ program for multilevel Inheritance.


#include<iostream.h>
#include<conio.h>
class student
{
int rollno;
char course[10];
public:
void getdata()
{
cout<<"\n Enter your course and rollno=";
cin>>course>>rollno;
}
void putdata()
{
cout<<"\n Course="<<course;
cout<<"\n Roll No="<<rollno;
}
};
class test:public student
{
protected: int m1,m2;
public:
void getmarks()
{
cout<<"\n Enter marks of two subjects=";
cin>>m1>>m2;
}
void showmarks()
{
cout<<"\n Subject1 marks="<<m1;
cout<<"\n Subject2 marks="<<m2;
}
};
class result:public test
{
int total;
public:
void calc()
{
total=m1+m2;
putdata();
showmarks();
cout<<"\n Total Marks="<<total;
}
};
void main()
{
result r1;
clrscr();
r1.getdata();
r1.getmarks();
r1.calc();
getch();
}

C. Write a simple C++ program for multiple Inheritance.


#include<iostream.h>
#include<conio.h>

class student {
protected:
int rno, m1, m2;
public:

void get() {
cout << "Enter the Roll no :";
cin>>rno;
cout << "Enter the two marks :";
cin >> m1>>m2;
}
};

class sports {
protected:
int sm;
public:

void getsm() {
cout << "\nEnter the sports mark :";
cin>>sm;

}
};

class statement : public student, public sports {


int tot, avg;
public:

void display() {
tot = (m1 + m2 + sm);
avg = tot / 3;
cout << "\n\n\tRoll No : " << rno << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
}
};

void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}
7 Virtual function:
A. Implement the concept of method overriding.
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_code, age;
char name[30], qualification[30];
public:
void get()
{
cout<<"\nEnter employee id: ";
cin>>emp_code;
cout<<"\nEnter employee name: ";
cin>>name;
cout<<"\nEnter employee age: ";
cin>>age;
cout<<"\nEnter employee qualification: ";
cin>>qualification;
}
void show()
{
cout<<"\n\nEmployee id: "<<emp_code;
cout<<"\tName: "<<name;
cout<<"\nAge: "<<age<<"\t\tQualification: "<<qualification;
}
};
class contract_employee: public employee
{
int contract_id;
public:
void get()
{
cout<<"\nEnter contract_id: ";
cin>>contract_id;
}
void show()
{ cout<<"\nContract ID: "<<contract_id;
}
};
void main()
{
clrscr();
contract_employee ce;
ce.get();
ce.show();
getch();
}

B. Show the use of virtual function.


#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display()
{
cout<<"\nDisplay of base class called";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\nDisplay of derived class called";
}
};
void main()
{
clrscr();
base *b;
derived d;
b=&d;
b->display();
getch();
}

C. Show the implementation of abstract class.


#include<iostream.h>
#include<conio.h>
class Figure
{
public:
double dim1;
double dim2;
Figure(double a, double b)
{
dim1 = a; dim2 = b;
} // pure virtual function
virtual double area()=0;
};
class Rectangle:public Figure
{
public:
Rectangle(double a, double b):Figure(a,b)
{
} // implement area for rectangle
double area()
{
cout<<"\nInside Area for Rectangle:";
return dim1 * dim2;
}
};
class Triangle:public Figure
{
public:
Triangle(double a, double b):Figure(a,b)
{ }
// implement area for right triangle
double area()
{
cout<<"\nInside Area for Triangle:";
return dim1 * dim2 / 2;
}
};

void main()
{
clrscr();
Rectangle r(9, 5);
Triangle t(10, 8);
cout<< r.area();
cout<< t.area();
getch();
}

8 Exception handling:
A. Show the implementation of exception handling.
#include<iostream.h>
#include<conio.h>
void main()
{
float percent;
cout<<"Enter your percentage: ";
cin>>percent;
try
{
if(percent<0 || percent>100)
throw(percent);
else
cout<<endl<<"Your percentage: "<<percent;
}
catch(int p)
{
cout<<endl<<"Invalid percentage: "<<p;
}
}
9 File Handling:
A. Design a class FileDemo open a file in read mode and display
the total number of words and lines in the file.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
ifstream fread("WordLineCount.txt");
int wc=1,lc=1;
char c;
while(fread)
{
fread.get(c);
if(c==' '|| c=='\n')
wc++;
if(c=='\n')
lc++;
}
fread.close();
cout<<"\n Total no. of words in the file: "<<wc;
cout<<"\n Total no. of lines in the file: "<<lc;
getch();
}

B. Design a class to handle multiple files and file operations.


#include<iostream.h>
#include<conio.h>
#include<fstream.h>

void main()
{
clrscr();
ofstream fwrite("Alphabets.txt");
fwrite<<"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
fwrite.close();
ifstream fread("Alphabets.txt");
ofstream fwrite1("Vowels.txt");
ofstream fwrite2("Consonants.txt");
char c;
while(fread)
{
fread.get(c); if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U') fwrite1<<c; else fwrite2<<c;
}
fread.close();
fwrite1.close();
fwrite2.close();
fread.open("Alphabets.txt");
ifstream fread1("Vowels.txt");
ifstream fread2("Consonants.txt");
cout<<"\n\nContents of Alphabets File\n";
cout<<"--------------------------\n";
while(fread)
{
fread.get(c);
cout<<c;
}
fread.close();
cout<<"\n\nContents of Vowels File\n";
cout<<"-----------------------\n";
while(fread1)
{
fread1.get(c);
cout<<c;
}
fread1.close();
cout<<"\n\nContents of Consonants File\n";
cout<<"---------------------------\n";
while(fread2)
{
fread2.get(c);
cout<<c;
}
fread2.close();
getch();
}