LIST OF EXERCISES:
1
|
Program to illustrate inline function and function overloading
|
`2
|
Program to illustrate function overloading to calculate area of circle, rectangle and square
|
3
|
Program to illustrate access specifies i.e : public, private, protected member variables and methods
|
4
|
Program to illustrate friend function
|
5
|
Program to illustrate virtual function
|
6
|
Program to illustrate default constructor, parameterized constructor and copy constructors
|
7
|
Program to illustrate destructors
|
8
|
Program to illustrate single inheritance ,multiple inheritance ,multi level inheritance ,hybrid inheritance
|
9
|
Program to illustrate virtual base class
|
10
|
Program to illustrate run time polymorphism, compile time polymorphism, run time polymorphism
|
11
|
Program to illustrate pointer using this operator
|
12
|
Program to illustrate Operator overloading
a)Unary Operator b)Binary Operator
|
13
|
Program to illustrate Exception handling ?Mechanisms using try, catch, throw keywords
|
14
|
Program to illustrate New and Delete keywords for dynamic memory allocation
|
15
|
Program to illustrate formatted and unformatted I/O streams
|
16
|
Program to illustrate template classes
|
17
|
Program to illustrate file operations
|
18
|
Program to illustrate I/O manipulators
|
write a program to illustrate inline function
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
cout<<"\n\nCube value is :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}
OUTPUT:
Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343
write a program to illustrate function overloading
#include<iostream.h>
int add (int, int);
int add (int, int, int);
void main ()
{
int a,b,c;
cout<<”enter a,b,c values”;
cin>>a>>b>>c;
cout<<”addition of a,b is:”<<add(a,b)<<endl;
cout<<”addition of a,b,c is:’<<add(a,b,c)<<endl;
}
int add (int a, int b)
{
return (a+b);
}
int add (int a, int b, int c)
{
return (a+b+c);
}
OUTPUT:
enter a,b,c values 1 2 3
addition of a,b is : 3
addition fo a,b,c is : 6
Program to illustrate function overloading to calculate area of circle,rectangle and sqare
#include<iostream.h>
#include<conio.h>
int area(int);
int area(int,int);
float area(float);
void main()
{
clrscr();
cout<<"AreaOfSquare:"<<area(4);
cout<<"AreaOfRectangle:"<<area(4,4);
cout<<"AreaOfCircle:"<<area(3.2);
getch();
}
int area(int a)
{
return(a*a);
}
int area(int a,int b)
{
return(a*b);
}
float area(float r)
{
return(3.14*r*r);
}
OUTPUT:
Area Of Square: 16 Area Of Rectangle: 16 Area Of Circle: 10.048
Program to illustrate access specifies i.e: public, private, protected member variables and methods
#include<iostream.h>
class A
{
private:
int a;
protected:
int b
public:
void get()
{
cout<<”enter a,b values”;
cin>>a>>b;
}
public:
void display()
{
cout<<”a,b values are:’<<a<<b;
}
};
void main
{
A obj;
obj.get()
obj.display();
}
OUTPUT:
enter a,b vaues 2 2
a,b vaues are : 2 2
Program to illustrate friend function
#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend int add(base ob);
};
int mean(base ob)
{
return add(ob.val1+ob.val2);
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\n addition value is : "<<add(obj);
getch();
}
OUTPUT:
Enter two values: 10, 20
addition Value is: 30
Program to illustrate virtual function
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display()
{
cout<<"\n Base class display:" ;
}
};
class derive : public base
{
public:
void display()
{
cout<<"\n derive class display:";
}
};
void main()
{
clrscr();
base *p;
derive obj;
p=&obj;
p->display();
}
OUTPUT:
derive class display
Program to illustrate default constructor
#include <iostream.h>
class defal
{
public:
int x;
int y;
defal()
{
x=1;
y=2;
}
void display()
{
cout<<”x,y vaues are:”<<x<<y;
}
};
int main()
{
defal obj;
obj.display();
}
OUTPUT:
x,y values are : 1 2
Program to illustrate default Parameterized constructor
#include<iostream>
#include<conio.h>
using namespace std;
class Example
{
int a,b;
public:
Example(int x,int y)
{
a=x;
b=y;
}
void Display()
{
cout<<"Values :"<<a<<"\t"<<b;
}
};
int main()
{
Example Object(10,20);
Object.Display();
getch();
return 0;
}
OUTPUT:
Values :10 20
Program to illustrate default copy constructor
#include <iostream.h>
class defal
{
public:
int x;
int y;
defal()
{
x=1;
y=2;
}
void display()
{
cout<<”x,y vaues are:”<<x<<y;
}
};
int main()
{
defal obj;
obj.display();
defal obj1=obj;
obj1.display();
}
OUTPUT:
x,y values are : 1 2
x,y values are : 1 2
Program to illustrate destructorsProgram:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void set(int b);
void add();
A();
~A();
};
A::A()
{
cout<<"constructor"<<endl;
}
A::~A()
{
cout<<"destructor"<<endl;
}
void A:: set(int b)
{
a=b;
cout<<"a value is :"<<a<<endl;
}
void A::add()
{
int x=2,y=2,z;
z=x+y;
cout<<"addition is: "<<z<<endl;
}
void main()
{
clrscr();
A obj;
obj.set(2);
obj.add();
}
OUTPUT:
constructor
a value is : 2
addition is : 4
destructor
Program to illustrate single inheritance
#include <iostream.h>
class Value
{
protected:
int val;
public:
void set_values (int a)
{
val=a;
}
};
class Cube: public Value
{
public:
int cube()
{
return (val*val*val); }
};
int main ()
{
Cube cub;
cub.set_values (5);
cout << "The Cube of 5 is::" << cub.cube() << endl;
return 0;
}
OUTPUT:
The Cube of 5 is:: 125
Program to illustrate 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();
}
OUTPUT:
Enter the Roll no: 100
Enter two marks
90
80
Enter the Sports Mark: 90
Roll No: 100
Total : 260
Average: 86.66
Program to illustrate multi level inheritance
#include <iostream.h>
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{
rollno = a;
}
void put_num()
{
cout << "Roll Number Is:\n"<< rollno << "\n";
}
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class res : public marks
{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2;
put_num();
put_marks();
cout << "Total:"<< tot;
}
};
int main()
{
res std1;
std1.get_num(5);
std1.get_marks(10,20);
std1.disp();
return 0;
}
OUTPUT:
Roll Number Is:
5
Subject 1: 10
Subject 2: 20
Total: 30
Program to illustrate Hybrid inheritance
#include <iostream.h>
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{
rollno = a;
}
void put_num()
{
cout << "Roll Number Is:"<< rollno << "\n"; }
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class extra
{
protected:
float e;
public:
void get_extra(float s)
{
e=s;
}
void put_extra(void)
{
cout << "Extra Score::" << e << "\n";
}
};
class res : public marks, public extra
{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2+e;
put_num();
put_marks();
put_extra();
cout << "Total:"<< tot;
}
};
int main()
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}
OUTPUT:
Roll Number Is: 10
Subject 1: 10
Subject 2: 20
Extra score:33.12
Total: 63.12
Program to illustrate virtual base class
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};
class test: virtual public student
{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tPart1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
class sports:public virtual student
{
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout<<"\n\tSports Score is:"<<score;
}
};
class result:public test,public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\tTotal Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
OUTPUT:
Enter Roll No: 200
Enter Marks
Part1: 90
Part2: 80
Enter Sports Score: 80
Roll No: 200
Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250
Program to illustrate Run time polymorphism
class Base
{
public:
virtual void display(int i)
{
cout<<"Base::"<<i;
}
};
class Derv: public Base
{
public:
void display(int j)
{
cout<<"Derv::"<<j;
}
};
int main()
{
Base *ptr=new Derv;
ptr->display(10);
return 0;
}
OUTPUT:
Derv::10
Program to illustrate pointer using this operator
#include< iostream>
using namespace std;
struct X
{
private:
int a;
public:
void Set_a(int a)
{
this->a = a;
}
void print_a()
{
cout << "a = " << a << endl; }
};
int main() {
X xobj;
int a = 5;
xobj.Set_a(a);
xobj.print_a();
}
OUTPUT:
a=5
Program to illustrate Binary Operator
#include<iostream.h>
#include<conio.h>
class binary
{
int n;
public:
void get()
{
cout<<"enter n value";
cin>>n;
}
binary operator+(unary obj)
{
unary t;
t.n=n+obj.n;
return(t);
}
void display()
{
cout<<”after addition “<<n;
}
};
void main()
{
binary a1,a2,a3;
a1.get();
a2.get();
a3=a1+a2;
a3.display();
}
enter n value 2
after addition 4
Program to illustrate Unary Operator
#include<iostream.h>
#include<conio.h>
class unary
{
int a,b;
public:
{
void get()
{
cout<<”enter a,b values”;
cin>>a>>b;
}
void operator++()
{
a=++a;
b=++b;
}
void display();
{
cout<<”after incrementing a,b are”<<a<<b;
}
};
void main()
binary obj;
obj.get();
obj++;
obj.display();
}
OUTPUT:
enter a,b values : 2 2
after incrementing a,b are 3 3
Program to illustrate Exception handling Mechanisms using try, catch, throw keywords
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
float d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
cout<<"Answer is infinite because a-b is:"<<i;
}
getch();
}
OUTPUT:
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40
Answer is infinite because a-b is: 0
Program to illustrate New and Delete keywords for dynamic memory allocation
#include< iostream.h>
#include<conio.h>
int main()
{
clrscr();
int * ptr = new int;
float *q=new float;
*ptr = 5;
*q=5.0;
cout<< *ptr <<endl;
cout<<*q<<endl;
delete ptr;
cout<<"deleted";
return 0;
}
OUTPUT:
5
5
5
deleted
#include<iostream.h>
#include<conio.h>
void main()
{
cout.setf(ios::showpoint);
cout<<setw(5)<<”n \n”;
cout<<setw(15)<<”inverse of \n”;
cout<<setw(15)<<”sum of terms \n”;
double term, sum=0;
for(int n=1;n<=2;n++)
{
sum=1.0/float(n);
temp=temp+sum;
cout<<sew(5)<<n;
cout<<setw(14)<<setprecision(4);
cout<<setiosflags(ios::scientific)<<term;
cout<<setw(13)<<resetiosflags(ios::scientific);
cout<<sum<<endl;
}
}
OUTPUT:
n inverse of sum of terms
1 1.0000e+000 1.0000
2 5.0000e-001 1.5000
Program to illustrate Template Classes
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t &x,t &y)
{
t temp=x;
x=y;
y=temp;
}
void fun(int a,int b,float c,float d)
{
cout<<"enter a,b,c,d";
cin>>a>>b>>c>>d;
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<c<<"\t"<<d;
}
void main()
{
int a,b;
float c,d;
clrscr();
fun(a,b,c,d);
getch();
}
OUTPUT:
enter a,b,c,d 1 2 3 4
enter a,b,c,d 1 2 3 4
a and b before swaping 1 2
a and b after swaping 2 1
c and d before swaping 3 4
c and d after swaping 4 3
Program to illustrate File Operations
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class student
{
private:
int rno;
char name[10];
private:
int rno;
char name[10];
float fees;
public:
void getdata()
{
cout<<"roll number";
cin>>rno;
cout<<endl;
cout<<"enter name:";
cin>>name;
cout<<endl<<"enter fees:";
cin>>fees;
}
void dispdata()
{
cout<<"Roll number"<<rno<<endl;
cout<<"Name"<<name<<endl;
cout<<"Fees"<<fees;
}
};
void main()
{
student s1;
clrscr();
ofstream stdfile("c:\\std.txt");
//fstream stdfile;
//stdfile.open("c:\\std.txt",ios::out|ios::in); //open file for output
char wish;
//writing to the file
do
{
s1.getdata();
stdfile.write((char*)&s1,sizeof(student));
cout<<"continue ? y/n";
cin>>wish;
}
while(wish=='y'||wish=='Y');
stdfile.close(); //close the file
getch();
}
OUTPUT:
Rollnumber 121
Entername Jacob
Enter fees 10000
Program to illustrate I/O Manipulators
#include <iostream.h>
void main (void)
{
int value;
cout << “ Enter number’ << endl;
cin >> value;
cout << “ Decimal base = “ << dec << value << endl;
cout << “ Hexadecimal base = “ << hex << value << endl;
cout << “ Octal base = “ << oct << value << endl;
}
OUTPUT:
Enter number
Enter number
10
Decimal base = 10
Hexadecimal base = a
Octal base = 12
write a program to illustrate inline function
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
cout<<"\n\nCube value is :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}
OUTPUT:
Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343
write a program to illustrate function overloading
#include<iostream.h>
int add (int, int);
int add (int, int, int);
void main ()
{
int a,b,c;
cout<<”enter a,b,c values”;
cin>>a>>b>>c;
cout<<”addition of a,b is:”<<add(a,b)<<endl;
cout<<”addition of a,b,c is:’<<add(a,b,c)<<endl;
}
int add (int a, int b)
{
return (a+b);
}
int add (int a, int b, int c)
{
return (a+b+c);
}
OUTPUT:
enter a,b,c values 1 2 3
addition of a,b is : 3
addition fo a,b,c is : 6
Program to illustrate function overloading to calculate area of circle,rectangle and sqare
#include<iostream.h>
#include<conio.h>
int area(int);
int area(int,int);
float area(float);
void main()
{
clrscr();
cout<<"AreaOfSquare:"<<area(4);
cout<<"AreaOfRectangle:"<<area(4,4);
cout<<"AreaOfCircle:"<<area(3.2);
getch();
}
int area(int a)
{
return(a*a);
}
int area(int a,int b)
{
return(a*b);
write a program to illustrate inline function
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
cout<<"\n\nCube value is :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}
OUTPUT:
Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343
write a program to illustrate function overloading
#include<iostream.h>
int add (int, int);
int add (int, int, int);
void main ()
{
int a,b,c;
cout<<”enter a,b,c values”;
cin>>a>>b>>c;
cout<<”addition of a,b is:”<<add(a,b)<<endl;
cout<<”addition of a,b,c is:’<<add(a,b,c)<<endl;
}
int add (int a, int b)
{
return (a+b);
}
int add (int a, int b, int c)
{
return (a+b+c);
}
OUTPUT:
enter a,b,c values 1 2 3
addition of a,b is : 3
addition fo a,b,c is : 6
Write a program for implementing manpulators
main()
{
{
int a,b;
a = 200;
b = 300;
cout << setfill ( ‘ * ’ );
cout << setw (5) << a<< setw (5) << b << endl;
cout << setw (6) << a<< setw (6) << b << endl;
cout << setw (7) << a<< setw (7) << b << endl;
cout << setw (8) << a<< setw (8) << b << endl;
}
OUTPUT:
**200**300
***200***300
****200****300
*****200*****300
- Data structures using C language training in Hyderabad
- Data structures using C language training course
- online training for Data structures using C language
- online training of Data structures using C language
- Data structures using C language training software
- JNTU Data structures using C language syllabus online training
- OSMANIA UNIVERSITY Data structures using C language syllabus online training
- Data structures using C language with in two months
- Data structures using C language with in one month
- Data structures using C language with in few days
- A to Z about Data structures using C language
Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.
No comments:
Post a Comment