Documente Academic
Documente Profesional
Documente Cultură
}
};
void main()
{
student s1;
clrscr();
s1.getdata();
s1.putdata();
getch();
}
};
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();
};
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);
}
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;
}
// 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) {
void main()
{
clrscr();
abc a1;
a1.get();
cout<<"\n\n Original contents";
a1.show();
-a1;
cout<<"\n\n After Negation";
a1.show();
getch();
}
{
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);
}
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;
}
};
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();
}
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();
}
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();
}