CPP Lab
WAP using C to calculate grade of a student (name, no, 3 marks) using structures and
user defined functions
input(), calcGrade(), display().
/****************************************************************************
***************************
18/06/2016
******************************************************************************
*************************/
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20],grade;
int rno;
float m1,m2,m3,per;
}s;
void input()
{
printf("\nEnter the name :");
gets(s.name);
printf("Enter the roll number :");
scanf("%d",&s.rno);
printf("Enter the mark for 3 subjects :");
scanf("%f%f%f",&s.m1,&s.m2,&s.m3);
}
void calcGrade()
{
float tot;
CPP Lab
int p;
tot=(s.m1 + s.m2 + s.m3);
s.per=(float)(tot/3);
p=s.per;
if(p>=80)
{
s.grade='A';
}
else if(p>=70 && p<80)
{
s.grade='B';
}
else if (p>=60 && p<70)
{
s.grade='C';
}
else if (p>=50 && p<60)
{
s.grade='D';
}
else
{
s.grade='F';
}
}
void display()
{
printf("\n\n\tSTUDENT DETAILS");
printf("\n\t---------------");
printf("\nName : %s",s.name);
printf("\nRoll Number : %d",s.rno);
printf("\nMarks : %5.3f\t%5.3f\t%5.3f",s.m1,s.m2,s.m3);
printf("\nPercentage : %f",s.per);
printf("\nGrade : %c",s.grade);
}
CPP Lab
void main()
{
clrscr();
input();
calcGrade();
display();
getch();
}
CPP Lab
Create a class of your choice. Add proper data members and member functions. Create
object and display the values.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class student
{
char name[20],grade;
int rno;
float m1,m2,m3;
public :
void input();
void calcGrade();
void display();
}s;
void student :: input()
{
cout<<"Enter the name :";
cin>>s.name;
cout<<"Enter the roll number :";
cin>>s.rno;
cout<<"Enter the mark for 3 subjects :";
cin>>s.m1>>s.m2>>s.m3;
}
CPP Lab
void student ::calcGrade()
{
float tot,per;
int p;
tot=(s.m1 + s.m2 + s.m3);
per=(float)(tot/3);
p=per;
if(p>=80)
{
s.grade='A';
}
else if(p>=70 && p<80)
{
s.grade='B';
}
else if (p>=60 && p<70)
{
s.grade='C';
}
else if (p>=50 && p<60)
{
s.grade='D';
}
else
{
s.grade='F';
}
}
void student :: display()
{
cout<<"\nName : "<<s.name;
cout<<"\nRoll Number : "<<s.rno;
cout<<"\nMarks : "<<s.m1<<" "<<s.m2<<" "<<s.m3;
cout<<"\nGrade : "<<s.grade;
}
CPP Lab
void main()
{
clrscr();
s.input();
s.calcGrade();
s.display();
getch();
}
CPP Lab
WAP to calculate the area of different shapes like Rectangle, Square etc (at least 5
shapes) using overloaded function
area
().(Function overloading)
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class shape
{
float area;
public:
void fnarea(int);
void fnarea(int,int);
void fnarea(int,int,int);
void fnarea(float);
void fnarea(float,float);
}s;
void shape::fnarea(int x,int y,int z)
{
cout<<"\nArea:"<<x*y*z;
}
void shape:: fnarea(float x)
{
cout<<"\nArea :" <<3.14*x*x;
}
void shape :: fnarea(int x)
CPP Lab
{
cout<<"\nArea :"<<x*x;
}
void shape :: fnarea(int x, int y)
{
cout<<"\nArea :"<<x*y;
}
void shape :: fnarea(float x,float y)
{
cout<<"\nArea :"<< .5*x*y;
}
void main()
{
float a=4.5,b=5.3;
int x=6,y=7,z=5;
clrscr();
cout<<"\nCircle :";
s.fnarea(a);
cout<<"\n\nSquare :";
s.fnarea(x);
cout<<"\n\nRectangle :" ;
s.fnarea(x,y);
cout<<"\n\nTriangle :";
s.fnarea(a,b);
cout<<"\n\nBlock :";
s.fnarea(x,y,z);
getch();
}
OUTPUT
fi
§=DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
zs
ircle
:
:63.585
Area
:
210
CPP Lab
CPP Lab
WAP using a function
power
() to calculate m to the power of n. Use
default arguments
.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void power(int ,int);
void main()
{
int m,n;
clrscr();
cout<<"Enter value for n :";
cin>>n;
cout<<"Enter value for m :";
cin>>m;
power(n,m);
getch();
}
void power(int x=1,int n=1)
{
cout<<"Power : ";
cout<<pow(x,n);
}
OUTPUT
i
=DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
Ez
Enter
value
for
n
:10
Enter
value
for
m
:5
:
100000_
CPP Lab
CPP Lab
WAP using classes to perform bank transaction for n
customers
(cust_name, acc_no,
acc_type, balance). The program should be a menu driven & it should have the
following menus like adding new account, withdraw (keep a min balance of 500),
deposit, balance enquiry & account statement (cust_name, acc_no, acc_type, balance)
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class bank
{
char cust_name[20],acc_type;
int acc_no;
float balance;
public:
void add();
void withdraw(int);
void deposit(int);
void bal_enq(int);
void acc_stmt(int);
}b[20];
void bank :: add()
{
cout<<"Enter the name :" ;
cin>>cust_name;
cout<<"Enter account type :";
cin>>acc_type;
CPP Lab
cout<<"Enter account number :";
cin>>acc_no;
cout<<"Enter amount :";
cin>>balance;
}
void bank :: withdraw(int n)
{
float amt,bal;
int acno,i,f;
cout<<"Enter account number:";
cin>>acno;
for(i=0;i<n;i++)
{
if(b[i].acc_no==acno)
{
cout<<"Enter amount :";
cin>>amt;
bal=b[i].balance-amt;
if(bal>500)
{
b[i].balance=bal;
cout<<"Amount Withdrawed!!!";
}
else
{
cout<<"Amount cannot be withdrawed";
}
f=1;
}
}
if(f==0)
cout<<"Accont not found!!";
}
void bank :: deposit(int n)
CPP Lab
{
float amt,bal;
int acno,i,f;
cout<<"Enter account number:";
cin>>acno;
for(i=0;i<n;i++)
{
if(b[i].acc_no==acno)
{
cout<<"Enter amount :";
cout<<"Enter amount :";
cin>>amt;
b[i].balance=b[i].balance+amt;
cout<<"Amount Deposited!!!";
}
}
}
void bank:: bal_enq(int n)
{
int acno,i,f;
cout<<"Enter account number:";
cin>>acno;
for(i=0;i<n;i++)
{
if(b[i].acc_no==acno)
{
cout<<"Balance :"<<b[i].balance;
}
}
}
void bank:: acc_stmt(int n)
{
int acno,i,f;
cout<<"Enter account number:";
cin>>acno;
CPP Lab
for(i=0;i<n;i++)
{
if(b[i].acc_no==acno)
{
cout<<"\nAccount Number : "<<b[i].acc_no;
cout<<"\nName : "<<b[i].cust_name;
cout<<"\nAccount Type : "<<b[i].acc_type;
cout<<"\nBalance : "<<b[i].balance;
}
}
}
void main()
{
bank s;
int c,i,n;
clrscr();
cout<<"Enter the number of customers :" ;
cin>>n;
cout<<"Enter details of customers :\n";
for(i=0;i<n;i++)
{
b[i].add();
}
cout<<"\n1.Money Withdrawal\n2.Money Deposit\n3.Balance Enquiry\n4.Account
Statement\nEnter your choice: ";
cin>>c;
switch(c)
{
case 1: s.withdraw(n);
break;
case 2:s.deposit(n);
break;
case 3: s.bal_enq(n);
break;
case 4: s.acc_stmt(n);
CPP Lab
break;
}
getch();
}
CPP Lab
WAP to add two
distance
objects using a member function. (objects as arguments to
member function)
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class distance
{
int km,m;
public :
void getdata()
{
cout<<"Enter the distance in km and m :";
cin>>km>>m;
}
void putdata()
{
cout<<"Distance : "<<km<<" km "<<m<<" m";
}
void add(distance);
};
void distance :: add(distance d1)
{
m=m+d1.m;
km=km+d1.km;
if(m>1000)
CPP Lab
{
km=km+(m/1000);
m=m%1000;
}
}
void main()
{
distance d1,d2;
clrscr();
d1.getdata();
d2.getdata();
d2.add(d1);
d2.putdata();
getch();
}
CPP Lab
WAP that calculates the number of objects created for a class. (static data members &
member functions)
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class a
{
static int i;
public :
void objcall()
{
i++;
}
friend void putvalue();
};
int a :: i;
void putvalue()
{
a D;
cout<<"Number of object created : "<<D.i;
}
void main()
{
a A,B,C,D;
CPP Lab
clrscr();
A.objcall();
B.objcall();
C.objcall();
D.objcall();
putvalue();
getch();
}
CPP Lab
WAP to perform arithmetic operations of
fraction
objects. Add/Sub/Mul/Div by using
member function . Use objects are arguments & return object. (objects as arguments
member)
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class fraction
{
int a,b;
public:
void getdata();
void putdata();
void add(fraction,fraction);
void sub(fraction,fraction);
void mult(fraction,fraction);
void div(fraction,fraction);
};
void fraction :: getdata()
{
cout<<"Enter the fractional value :" ;
cin>>a>>b;
}
void fraction ::putdata()
{
CPP Lab
cout<<a<<"/"<<b;
}
void fraction :: add(fraction f1,fraction f2)
{
if(f1.b==f2.b)
{
a=f1.a+f2.a;
}
else
{
a=f1.a*f2.b+f2.a*f1.b;
b=f1.b*f2.b;
}
}
void fraction :: sub(fraction f1,fraction f2)
{
if(f1.b==f2.b)
{
a=f1.a-f2.a;
}
else
{
a=(f1.a*f2.b)-(f2.a*f1.b);
b=f1.b*f2.b;
}
}
void fraction :: mult(fraction f1,fraction f2)
{
a=f1.a*f2.a;
b=f1.b*f2.b;
}
void fraction ::div(fraction f1,fraction f2)
{
a=f1.a*f2.b;
b=f1.b*f2.a;
CPP Lab
}
void main()
{
fraction f1,f2,f3;
clrscr();
f1.getdata();
f2.getdata();
f3.add(f1,f2);
cout<<"\nAdd\n------\n Result : ";
f3.putdata();
f3.sub(f1,f2);
cout<<"\nSubtract\n-------\n Result : ";
f3.putdata();
f3.mult(f1,f2);
cout<<"\nProduct\n--------\n Result : ";
f3.putdata();
f3.div(f1,f2);
cout<<"\nDivide\n---------\n Result : ";
f3.putdata();
getch();
}
OUTPUT
fi
DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
Zz
Enter
the
fractional value
:3
5
Enter
the
fractional
value
:1
3
14/715
>:
4715
:
3715
:
975
CPP Lab
CPP Lab
WAP to perform operations such as compare (member function), concatenate, length (friend
function) on
String
objects.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class String
{
char a[10];
public:
void getvalue()
{
gets(a);
}
void putvalue()
{
cout<<"\n";
puts(a);
}
int compare(String);
String concatenate(String);
friend void length(String,String);
};
int String :: compare(String s)
CPP Lab
{
int c;
c=strcmp(a,s.a);
if (c==0)
return 1;
else
return 0;
}
String String :: concatenate(String s)
{
strcat(a,s.a);
return s;
}
void length(String s1,String s2)
{
int l1,l2;
l1=strlen(s1.a);
l2=strlen(s2.a);
cout<<"\nLength of First String : "<<l1<<"\nLength of second string : "<<l2;
}
void main()
{
String s1,s2;
int c;
clrscr();
s1.getvalue();
s2.getvalue();
c=s1.compare(s2);
length(s1,s2);
if(c==1)
cout<<"Strings are Equal";
else
cout<<"Strings are not equal";
s1.concatenate(s2);
s1.putvalue();
OUTPUT
fi
DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
Enter
String
:
Apple
Enter
String
:
Orange
Length
of
First
String
:
5
Length
of
second
string
:
6
Strings
are
not
equal
yeeiit
TC
CPP Lab
getch();
}
CPP Lab
Design two classes
DMC
&
DFI
that store distances in m/cm & ft/in respectively. WAP
using friend functions that can add one object of DMC with another object of DFI..
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include <iostream.h>
#include <conio.h>
class dfi;
class dmc
{
int m,cm;
public:
void getDist()
{
cout<<"\nEnter distance(m/cm)";
cin>>m>>cm;
}
friend void sumDist(dmc,dfi);
};
class dfi
{
int ft,inch;
public:
void getDist()
{
cout<<"\nEnter distance(ft/in)";
cin>>ft>>inch;
CPP Lab
}
friend void sumDist(dmc,dfi);
};
void sumDist(dmc x,dfi y)
{
x.m=x.m+(y.ft*30);
x.cm=x.cm+(y.inch*2.5);
cout<<"Total distance is\n"<<x.m<<"m"<<x.cm<<"cm";
}
void main()
{
dmc m;
dfi f;
clrscr();
m.getDist();
f.getDist();
sumDist(m,f);
getch();
}
CPP Lab
Define class
String
& use constructors to initialize null string, assign a string to data
member, copy one string to another. Use destructor to reset string objects.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class String
{
char a[20];
public:
String()
{
strcpy(a,'\0');
}
String(char s[10])
{
strcpy(a,s);
}
String(String &s)
{
strcpy(a,s.a);
}
void putdata()
{
CPP Lab
cout<<"\n"<<a;
}
~String()
{
cout<<"\nDestructor cleared..." <<strcpy(a,'\0');
}
};
void main()
{
clrscr();
String s1;
s1.putdata();
String s2("Hello");
s2.putdata();
String s3(s2);
s3.putdata();
getch();
}
CPP Lab
WAP to demonstrate the order of execution of constructors & destructors (global & local).
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class sample
{
public:
sample()
{
cout<<"\nConstructor invoked...";
}
~sample()
{
cout<<"\nDestructor invoked..." ;
}
} ;
sample s;
void main()
{
clrscr();
sample s1,s2;
{
sample s3;
}
sample s4;
OUTPUT
i
=DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
Ez
onstructor
invoked...
onstructor
invoked...
onstructor
invoked...
Destructor
invoked...
onstructor
invoked...
Destructor
invoked...
Destructor
invoked...
Destructor
invoked...
Destructor
invoked..._
CPP Lab
getch();
}
CPP Lab
WAP to perform matrix multiplication for a dynamically constructed arrays (use new &
delete)
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class Matrix
{
int **m, p, q;
public:
Matrix();
Matrix(int);
// Matrix multiply(Matrix);
void multiply(Matrix);
void read();
void display();
~Matrix();
};
Matrix::Matrix()
{
int i;
cin>>p>>q;
m=new int*[10];
for(i=0;i<=q;i++)
m[i] =new int[5];
};
CPP Lab
Matrix::Matrix(int c)
{
m=new int*[10];
int i;
for(i=0;i<=c;i++)
m[i]=new int[5];
}
void Matrix::display()
{
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
cout<<m[i][j]<<"\t";
cout<<"\n";
}
}
void Matrix::read()
{
for(int i = 0; i < p;i++)
for(int j = 0;j < q;j++)
cin>>m[i][j];
}
void Matrix::multiply(Matrix ob1)
{
int s[10][10];
for(int i = 0;i < ob1.p;i++)
for(int j = 0;j < q; j++)
{
s[i][j] = 0;
for(int k = 0;k < p;k++)
s[i][j] = s[i][j] + m[i][k] * ob1.m[k][j];
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
CPP Lab
{
m[i][j]=s[i][j];
}
}
}
Matrix::~Matrix()
{
delete m;
}
int main()
{
int r,c;
clrscr();
cout<<"\nEnter the order of matrix 1: ";
Matrix ob1;
cout<<"\nEnter the elements of matrix 1: ";
ob1.read();
cout<<"\nEnter the order of matrix 2: ";
Matrix ob2;
cout<<"\nEnter the elements of matrix 2: ";
ob2.read();
cout<<"\nMatrix 1 :\n";
ob1.display();
cout<<"\nMatrix 2 :\n";
ob2.display();
ob2.multiply(ob1);
cout<<"\nResult matrix = \n";
ob2.display();
getch();
return 1;
}
OUTPUT
fl
DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
Ea
nter
the
order
of
matrix
1:
2 2
nter
the
elements
of
matrix
1:
11
nter
the
order
of
matrix
2:
22
nter
the
elements
of
matrix
2:
11
CPP Lab
CPP Lab
Create a class called
stack
with suitable constructors & member functions.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define max 10
class stack
{
int *a;
public:
stack()
{
a=new int[max];
}
int push(int i)
{
if(i<max)
{
i++;
cout<<"Enter the value : ";
cin>>a[i];
}
else
{
cout<<"Stack Overflow";
CPP Lab
}
return i;
}
int pop(int i)
{
if(i>-1)
{
cout<<"Element "<<a[i]<<" popped out...";
i--;
}
else
{
cout<<"Stack Underflow";
}
return i;
}
void display(int i)
{
cout<<"\n|"<<a[i]<<"|";
}
~stack()
{
delete []a;
}
};
void main()
{
stack s;
int ch,t=-1,i;
char c;
clrscr();
do{
cout<<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\nEnter your choice : ";
cin>>ch;
switch(ch)
CPP Lab
{
case 1:t=s.push(t);
break;
case 2:t=s.pop(t);
break;
case 3:for(i=t;i>=0;i--)
{
s.display(i);
}
break;
case 4: exit(0);
}
cout<<"Do you want to continue?";
cin>>c;
}while(c=='y'||c=='Y');
getch();
}
CPP Lab
Overload operators (<=, >= , ==) on
string
objects.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class operover
{
char s[10];
public :
void getdata()
{
cout<<"Enter the string : ";
cin>>s;
}
int operator<=(operover);
int operator>=(operover);
int operator==(operover);
};
int operover :: operator<=(operover o)
{
if(strcmp(s,o.s)<0)
{
return 1;
}
else
CPP Lab
{
return 0;
}
}
int operover :: operator>=(operover o)
{
if(strcmp(s,o.s)>0)
{
return 1;
}
else
{
return 0;
}
}
int operover :: operator ==(operover o)
{
if(strcmp(s,o.s)==0)
{
return 1;
}
else
{
return 0;
}
}
void main()
{
clrscr();
operover o1,o2;
o1.getdata();
o2.getdata();
if(o1<=o2)
{
cout<<"String 1 is less than string 2";
CPP Lab
}
else if(o1>=o2)
{
cout<<"String 1 is greater than string 2";
}
else if(o1==o2)
{
cout<<"strings are equal";
}
getch();
}
CPP Lab
Perform a+=b by overloading operators on
complex
objects a & b
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public :
void getdata()
{
cout<<"Enter the complex number : ";
cin>>a>>b;
}
void operator +=(complex);
void putdata()
{
cout<<a<<"+i"<<b;
}
};
void complex :: operator +=(complex c)
{
a+=c.a;
b+=c.b;
}
void main()
CPP Lab
{
clrscr();
complex c1,c2;
c1.getdata();
c2.getdata();
cout<<"Complex Number :";
c1.putdata();
cout<<"\nComplex Number :";
c2.putdata();
c1+=c2;
cout<<"\nResult :";
c1.putdata();
getch();
}
CPP Lab
Perform d++ (to increment distance data members by 1 unit) and d+=10 (to increment
distance data members by 10 units on distance object d. Also perform arithmetic
operations (+ and -) on
distance
objects.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class distance
{
int cm,m;
public:
void getdata()
{
cout<<"Enter the length :";
cin>>m>>cm;
}
void operator ++()
{
m++;
cm++;
}
void operator +=(distance d)
{
m=d.m+10;
cm=d.cm+10;
}
CPP Lab
void putdata()
{
cout<<m<<" meter "<<cm<<" centimeters\n";
}
friend distance operator +(distance,distance);
friend distance operator -(distance,distance);
};
distance operator +(distance d,distance d1)
{
distance d2;
d2.m=d.m+d1.m;
d2.cm=d.cm+d1.cm;
return d2;
}
distance operator -(distance d,distance d1)
{
distance d2;
d2.m=d.m-d1.m;
d2.cm=d.cm-d1.cm;
return d2;
}
void main()
{
distance d,d1,d3;
clrscr();
d.getdata();
d1.getdata();
cout<<"Addition :\n";
d3=d+d1;
d3.putdata();
cout<<"Subtraction :\n";
d3=d-d1;
d3.putdata();
cout<<"Increment : \n";
d++;
CPP Lab
d1++;
cout<<"\n Distance + 1:\n";
d.putdata();
d1.putdata();
d1+=d;
cout<<"\n Distance + 10 :\n";
d1.putdata();
getch();
}
CPP Lab
Overload >> and << operators for
fraction
object.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class fraction
{
int a,b;
public:
friend istream& operator >>(istream&, fraction&);
friend ostream& operator <<(ostream&, fraction&);
};
istream& operator >> (istream& acc, fraction& f)
{
acc>>f.a;
acc>>f.b;
return acc;
}
ostream& operator << (ostream& dis, fraction &f)
{
dis<<f.a<<"/";
dis<<f.b;
return dis;
}
int main()
{
CPP Lab
fraction f1,f2;
clrscr();
cout<<"Enter two Fractions";
cin>>f1;
cin>>f2;
cout<<"Fraction numbers are :";
cout<<f1;
cout<<f2;
getch();
return 0;
}
CPP Lab
Menu driven program to overload ->, [], ()
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class oper
{
int x[3];
public:
int i;
oper * operator->()
{
return this;
}
int operator[](int l)
{
return x[l];
}
void init(int);
oper operator()(int );
void display()
{
cout<<i;
}
};
oper oper::operator()(int a)
CPP Lab
{
i=a;
return *this;
}
void oper ::init(int v)
{
static int si=0;
x[si]=v;
si++;
}
void main()
{
int l,v,ch;
oper o;
clrscr();
cout<<"1.[]\n2.->\n3.()\nEnter Choice :";
cin>>ch;
switch(ch)
{
case 1:
for(l=0;l<3;l++)
{
cout<<"Enter the value :";
cin>>v;
o.init(v);
}
for(l=0;l<3;l++)
{
cout<<o[l]<<"\t";
}
break;
case 2:
cout<<"Enter the value for I :";
cin>>v;
CPP Lab
o->i=v;
cout<<"Value of I :"<<o->i;
break;
case 3 :
cout<<"Enter the value :";
cin>>v;
o(v);
o.display();
break;
}
getch();
}
CPP Lab
x = 2 * y, perform the operation by overloaded * and = on
salary
objects x & y.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class salary
{
float sal;
public:
salary()
{
sal=0;
}
void getval()
{
cout<<"Enter the salary :";
cin>>sal;
}
friend int operator*(int,salary);
void operator=(int);
void display()
{
cout<<"Salary : "<<sal;
}
};
int operator*(int x,salary s)
CPP Lab
{
int c;
c=x*s.sal;
return c;
}
void salary::operator=(int x)
{
salary s;
s.sal=x;
s.display();
}
void main()
{
salary x,y;
clrscr();
y.getval();
x=2*y;
getch();
}
OUTPUT
fi
=DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
a
Enter
the
salary
:2345.65
Salary
:
4691
CPP Lab
CPP Lab
Program to overload new & delete for class
Complex
.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class complex
{
int *x,*y;
public:
void* operator new(size_t int );
complex()
{
x=new int();
y=new int();
}
void input()
{
cout<<"\nEnter the complex Number : ";
cin>>*x>>*y;
}
void display()
{
cout<<*x<<"\t"<<*y;
}
friend void operator delete(void *);
CPP Lab
~complex()
{
delete x;
delete y;
}
};
void* operator new(size_t a)
{
void *t;
t=malloc(a);
cout<<"\nMemory Allocated";
return t;
}
void operator delete(void *a)
{
free(a);
cout<<"\nDeleted";
}
void main()
{
clrscr();
complex c;
c.input();
c.display();
getch();
}
OUTPUT
f=
DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
zs
emory
Allocated
emory
Allocated
Enter
the
complex
Number
:
3 6
6
Deleted
Deleted
CPP Lab
CPP Lab
Menu driven program to perform all possible type conversions for classes
Feet
&
Inches
.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream>
#include<conio.h>
using namespace std;
class inches
{
public:
int inch;
void geti()
{
cout<<"Enter Inches :" ;
cin>>inch;
}
void puti()
{
cout<<"Inches :"<<inch;
}
};
class feet
{
int ft;
public:
feet()
{
CPP Lab
cout<<"Enter value :" ;
cin>>ft;
}
feet(int v) //basis to class
{
ft=v;
}
operator int() //class to basis
{
return ft;
}
void getf()
{
cout<<"Enter feet :" ;
cin>>ft;
}
void putf()
{
cout<<"Feet :"<<ft;
}
operator inches() //class to class
{
inches i;
i.inch=ft*12;
cout<<"Inches : "<<i.inch;
return i;
}
};
int main()
{
int v;
//clrscr();
cout<<"Basic to Class\n";
cout<<"--------------\n";
CPP Lab
cout<<"Enter the value :" ;
cin>>v;
feet f(v);
cout<<"Value is : ";
f.putf();
cout<<"\n\nClass to basic\n";
cout<<"--------------\n";
feet f1;
v=f1;
cout<<"Value is : "<<v;
cout<<"\n\nClass to Class";
cout<<"\n--------------\n";
inches i;
f1.getf();
i=f1;
return 0;
}
CPP Lab
Define Classes Student, Academics, Sports and Report , explain the concept of Virtual base
class on Hybrid inheritance.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class students
{
int rno;
char name[20];
public:
void getdet()
{
cout<<"Enter Roll Number : ";
cin>>rno;
cout<<"Enter the Name : ";
cin>>name;
}
void putdet()
{
cout<<"\nRoll Number : "<<rno;
cout<<"\nStudent Name : "<<name;
}
};
class academics : public virtual students
{
CPP Lab
int i;
float m;
public:
float tot;
academics()
{
tot=0;
}
void getmark();
void putmark()
{
cout<<"\nTotal Marks : "<<tot;
}
};
void academics :: getmark()
{
cout<<"Enter marks for 5 subjects (out of 100) : ";
for(i=0;i<5;i++)
{
cin>>m;
tot=tot+m;
}
}
class sports :public virtual students
{
public:
float spmark;
void getspmark()
{
cout<<"Enter the sports mark : ";
cin>>spmark;
}
void putspmark()
{
cout<<"\nSports Mark : "<<spmark;
CPP Lab
}
};
class report :public sports, public academics
{
float gtot,per;
public:
void calc()
{
gtot=tot+spmark;
per=gtot/5.5;
}
void display()
{
cout<<"\nGrand Total : "<<gtot;
cout<<"\nPercentage : "<<per;
}
};
void main()
{
report r[10];
int i,n;
clrscr();
cout<<"Enter the number of students : ";
cin>>n;
for(i=0;i<n;i++)
{
r[i].getdet();
r[i].getmark();
r[i].getspmark();
r[i].calc();
}
cout<<"\n\n STUDENT DETAILS";
cout<<"\n--------------------------";
for(i=0;i<n;i++)
{
CPP Lab
r[i].putdet();
r[i].putmark();
r[i].putspmark();
r[i].display();
cout<<"\n--------------------------";
}
getch();
}
CPP Lab
With suitable example, illustrate the concept of Multiple and Multilevel Inheritance including
Constructor with parameters.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
A(int x)
{
a=x;
}
void puta()
{
cout<<"\nA = "<<a;
}
};
class B :public A
{
int b;
public:
B(int x):A(x)
{
b=x;
CPP Lab
}
void putb()
{
cout<<"\nB = "<<b;
}
};
class C :public B
{
int c;
public:
C(int x):B(x)
{
c=x;
}
void putc()
{
cout<<"\nC = "<<c;
}
};
class D
{
int d;
public:
D(int x)
{
d=x;
}
void putd()
{
cout<<"\nD = "<<d;
}
};
class E :public D,public C
{
int e;
CPP Lab
public :
E(int x):D(x),C(x)
{
e=x;
}
void pute()
{
cout<<"\nE = "<<e;
}
};
void main()
{
E e1(5);
clrscr();
e1.puta();
e1.putb();
e1.putc();
e1.putd();
e1.pute();
getch();
}
OUTPUT
f=
DOSBox
0.74,
Cpu
speed:
max
100%
cycles,
Frameskip
0,
Program:
TC
-
zs
CPP Lab
CPP Lab
Define Classes Shapes, Circle, Square, Ellipse and Rectangle with member functions to get
the values for finding corresponding areas and print the same. Utilize the concept of Abstract
Class and Runtime polymorphism to solve the problem.
/****************************************************************************
***************************
Anoopa Baby
18/06/2016
******************************************************************************
*************************/
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
float area;
public:
virtual void getval()=0;
virtual void fnd_area()=0;
};
class circle :public shape
{
float r;
public:
void getval()
{
cout<<"Enter the radius : ";
cin>>r;
}
void fnd_area()
{
CPP Lab
area=3.14*r*r;
cout<<"Area = "<<area;
}
};
class square :public shape
{
int a;
public:
void getval()
{
cout<<"Enter the side : ";
cin>>a;
}
void fnd_area()
{
area=a*a;
cout<<"Area = "<<area;
}
};
class rectangle : public shape
{
float l,b;
public:
void getval()
{
cout<<"Enter the sides of rectangle :";
cin>>l>>b;
}
void fnd_area()
{
area=l*b;
cout<<"Area = "<<area;
}
};
class ellipse :public shape
CPP Lab
{
float x,y;
public:
void getval()
{
cout<<"Enter width and height of ellipse :";
cin>>x>>y;
}
void fnd_area()
{
area=3.14*x*y;
cout<<"Area = "<<area;
}
};
void main()
{
circle c;
square s;
ellipse e;
rectangle r;
shape *p;
clrscr();
cout<<"\nCIRCLE\n";
p=&c;
p->getval();
p->fnd_area();
cout<<"\nSQUARE\n";
p=&s;
p->getval();
p->fnd_area();
cout<<"\nELLIPSE\n";
p=&e;
p->getval();
p->fnd_area();
cout<<"\nRECTANGLE\n" ;
CPP Lab
p=&r;
p->getval();
p->fnd_area();
getch();
}