Вы находитесь на странице: 1из 57

ISE

4th semester
INFORMATION SCIENCE AND ENGINEERING
Program 1 : Define a class batsman with the following specifications:
Data members: batsman code, batsman name, innings, notout, runs,
batting average.
Member Function: to compute batting average (calcAvg()), read values
for data members and invoke the function calcAvg(). The function to display the
data members on the screen tabular form using manipulators and find out best
batsman. Declare calcAvg() as private member and invoke the same in read
function. (batsavg=runs/(innings-notout)).
#Program
#include<iostream>
#include<iomanip>
using namespace std;
class Batsman //class definition
{
private: int bcode; //data members
char bname[30];
float bavg,inn,nout,run;
float calcAvg(){ return(run/(inn-nout)); }
;
public : void readb(); //member functions
void displayb(int i=0);
int bestfind(Batsman b[],int n);
};
void Batsman::readb() //member function to read batsman details
{
cout<<"Batsman Code:";
cin>>bcode;
cout<<"Batsman Name:";
cin>>bname;
cout<<"Total Innings played:";
cin>>inn;
cout<<"Total innings that he was notout:";
cin>>nout;
cout<<"Total runs he made in "<<inn<<" Innings:";
cin>>run;
bavg=calcAvg(); //calling calcAvg() function
}

void Batsman::displayb(int i) //member function to display batsman details


{
if(i==0){
cout<<"|"<<left<<setw(13)<<bcode<<"|"<<left<<setw(13)<<bname<<"|"<<left<
<setw(8)<<inn<<"|"<<left<<setw(15)<<nout<<"|"<<left<<setw(11)<<run<<"|"<
<left<<setw(12)<<bavg<<"|"<<endl;
Page 1 of 56
else
{
cout<<endl<<"The best Batsman among the above listed with his Batting Average
"<<bavg<<" is "<<bname<<endl;
cout<<"And his Details are..."<<endl;
cout<<"Batsman code :"<<bcode<<endl;
cout<<"Batsman Name :"<<bname<<endl;
cout<<"Innings played :"<<inn<<endl;
cout<<"Notout matches :"<<nout<<endl;
cout<<"Total runs scored :"<<run<<endl;
cout<<"Batting Average :"<<bavg<<endl;
}
}

int Batsman::bestfind(Batsman b[],int n) //function to find best batsman


{
float bestavg[10];
for(int i=0;i<n;i++)
bestavg[i]=b[i].bavg;
int k;
float maxavg=0.00;
for(int i=0;i<n;i++)
if(bestavg[i]>=maxavg)
{
maxavg=bestavg[i];
k=i;
}
return(k);
}

int main()
{
Batsman b[20],besavg; //object declaration
cout<<"C++ Program to read and display the details of n number of batsmans in a
team and to find the best batsman based on his batting average."<<endl;
int i,n,k;
cout<<"Enter the number of Batsmans:";
cin>>n; //reading number of batsman
for(i=0;i<n;i++)
{
cout<<"_____________________________"<<endl;
cout<<"Enter the details of batsman "<<i+1<<endl;
b[i].readb(); //calling read function
}
Page 2 of 56
cout<<"_____________________________"<<endl;
cout<<"Here is the "<<n<<" Batsman details in tabular form."<<endl;

cout<<"_______________________________________________________________________________"<<endl;
cout<<"|Batsman code |Batsman Name |Innings |Notout matches |Total runs
|Batting Avg.|"<<endl;
cout<<"|_____________|_____________|________|_______________|___________|____________|"<<endl;
for(i=0;i<n;i++)
b[i].displayb(); //calling display function

cout<<"|_____________|_____________|________|_______________|___________|____________|"<<endl;
k=besavg.bestfind(b,n); //calling bestfind function
b[k].displayb(1); //calling to display function to display best batsman details
return(0);
} //end of main
Output:

Page 3 of 56
Program 2a : Write a C++ program to create a class called TIME. Accept two
valid times in the form hh:mm:ss, validate the time and display the result.
Page 4 of 56
Illustrate the use of objects as function arguments and perform the addition of
two times in the hours, minutes and seconds format. If the hour crosses more than
24,print in terms of days.
#Program
#include<iostream>
#include<iomanip>
using namespace std;
class TIME //class defination
{
private: int h,m,s,d; //data members
public : void read(); //member functions
void display();
TIME addtime(TIME T1,TIME T2);
};

void TIME::read() //read function to read time


{
int c=0;
do
{
if(c!=0)
cout<<"Invalid TIME"<<endl;
cout<<"Hour : ";
cin>>h;
cout<<"Minute : ";
cin>>m;
cout<<"Second : ";
cin>>s;
c++;
}while(h>23||m>59||s>59); //checking validity of time
}

void TIME::display() //function to display time


{
if(d==1)
cout<<"DAY 1 -> ";
cout<<h<<":"<<m<<":"<<s<<endl;
}

TIME TIME::addtime(TIME T1,TIME T2) //function to add two times


{
TIME T3;
T3.h=T1.h+T2.h; //adding two hours
T3.m=T1.m+T2.m; //adding two minutes
Page 5 of 56
T3.s=T1.s+T2.s; //adding two seconds
if(T3.s>59) //if sum of seconds is greater than 59
{
T3.m+=1; //increment minute
T3.s%=60; //modify second
}
if(T3.m>59) //if sum of minutes is greater than 59
{
T3.h+=1; //increment hour
T3.m%=60; //modify minute
}
if(T3.h>23) //if sum of hours is greater than 23
{
T3.d=1; //initialize day
T3.h%=24; //modify hour
}
return(T3);
}

int main()
{
cout<<"C++ Program to read,display and add two times by validating it."<<endl;
TIME T1,T2,T3; //object declaration
cout<<"__________________"<<endl;
cout<<"Enter Time for T1:"<<endl;
T1.read(); //read time 1
cout<<"__________________"<<endl;
cout<<"Enter Time for T2:"<<endl;
T2.read(); //read time 2
cout<<"__________________"<<endl;
cout<<"Time T1 : ";
T1.display(); //display time 1
cout<<"__________________"<<endl;
cout<<"Time T2 : ";
T2.display(); //display time 2
T3=T3.addtime(T1,T2); //calling two times to add two times
cout<<"__________________"<<endl;
cout<<"Sum of T1 and T2 is : ";
T3.display(); //display time 3
cout<<"__________________"<<endl;
return(0);
} //end of main
Output:

Page 6 of 56
Program 2b : Create a class by name COMPLEX. Write functions to read,
display and to negate the values of complex number. Overload ‘~’ symbol to
negate the values.
#Program
#include<iostream>
#include<iomanip>
#include<math.h>
Page 7 of 56
using namespace std;
class COMPLEX //class defination
{
private : float x,y; //data member
public : void read() //read function to read real and imaginary values
{
cin>>x>>y;
}
void display() //display complex number
{
cout<<x;
if(y<0)
cout<<"-i"<<fabs(y)<<endl;
else
cout<<"+i"<<y<<endl;
}
COMPLEX operator~(); //~operator function
};

COMPLEX COMPLEX::operator~() //~operator function defination


{
x=-x; //negating real value
y=-y; //negating imaginary value
}
int main()
{
cout<<"C++ Program to read, display and to negate the values of complex number
by overloading the operator '~'."<<endl;
COMPLEX C1,C2; //object declaration
cout<<"_____________________________"<<endl;
cout<<"Enter values for complex 1: ";
C1.read(); //read complex 1
cout<<"Enter values for complex 2: ";
C2.read(); //read complex 2
cout<<"_____________________________"<<endl;
cout<<"COMPLEX 1:";
C1.display(); //display complex 1
cout<<"COMPLEX 2:";
C2.display(); //display complex 2
cout<<"_____________________________"<<endl;
~C1; //negating complex 1
~C2; //negating complex 2
cout<<"After negating the values of complex numbers..."<<endl;
cout<<endl<<"COMPLEX 1:";
Page 8 of 56
C1.display(); //display complex 1
cout<<"COMPLEX 2:";
C2.display(); //display complex 2
cout<<"_____________________________"<<endl;
return(0);
} //end of main

Output:

Program 3a : Write a C++ program to implement a simple calculator with


basic operations addition, subtraction, multiplication and division using classes
and objects. Use enumerated data type to select the operation and define all the
member functions as inline.
#Program
#include<iostream>
#include<iomanip>
Page 9 of 56
#include<stdlib.h>
using namespace std;
enum calcy{Add=1,Sub,Mul,Div,Ex}; //enum datatype
class calculator //class defination
{
private : float a,b; //data members
public : void read(); //member functions
int addnum();
int subnum();
int mulnum();
float divnum();
};

inline void calculator::read() //function to read two numbers


{
cout<<endl<<"Enter two numbers:";
cin>>a>>b;
}

inline int calculator::addnum() //function to add two numbers


{
return(a+b);
}

inline int calculator::subnum() //function to subtract two numbers


{
return(a-b);
}

inline int calculator::mulnum() //function to multiply two numbers


{
return(a*b);
}

inline float calculator::divnum() //function to divide two numbers


{
return(a/b);
}

int main()
{
cout<<"C++ Programing to implement simple calculator using enumerated datatype
and inline functions."<<endl;
calculator C; //object declaration
Page 10 of 56
int ch;
float res;
do //do while block
{
cout<<"___________________________"<<endl;

cout<<"1.Addition"<<endl<<"2.Subtraction"<<endl<<"3.Multiplication"<<endl<
<"4.Division"<<endl<<"5.Exit"<<endl<<"Enter your choice:";
cin>>ch; //reading choice
switch(ch) //switch block
{
case Add : C.read(); //calling read function
res=C.addnum(); //calling addnum function
cout<<"Addition result:"<<res<<endl;
break;
case Sub : C.read(); //calling read function
res=C.subnum(); //calling addnum function
cout<<"Subtraction result:"<<res<<endl;
break;
case Mul : C.read(); //calling read function
res=C.mulnum(); //calling addnum function
cout<<"Multiplication result:"<<res<<endl;
break;
case Div : C.read(); //calling read function
res=C.divnum(); //calling addnum function
cout<<"Division result:"<<res<<endl;
break;
case Ex : exit(1); //exit from program
default : cout<<"Invalid choice."<<endl;
} //end of switch block
}while(1); //end of do while block
return(0);
} //end of main
Output:

Page 11 of 56
Program 3b : Write a C++ Program that find the distance between two points
in 2D and 3D space using function overloading.
#Program
#include<iostream>
Page 12 of 56
#include<iomanip>
#include<math.h>
#include<stdlib.h>
using namespace std;
class dist //class defination
{
private: float x1,x2,y1,y2,z1,z2; //data members
public : float distance(float,float,float,float); //member functions
float distance(float,float,float,float,float,float);
};

float dist::distance(float x1,float x2,float y1,float y2)


{ //function to find distance in 2D plane
return(sqrt(pow((x2-x1),2)+pow((y2-y1),2)));
}

float dist::distance(float x1,float x2,float y1,float y2,float z1,float z2)


{ // function to find distance in 3D plane
return(sqrt(pow((x2-x1),2)+pow((y2-y1),2)+pow((z2-z1),2)));
}

int main()
{
cout<<"C++ Program to find distance between two points both in 2D and 3D
plane"<<endl;
dist d1; //object declaration
int ch;
float a,b,c,d,e,f;
do //do while block
{
cout<<"______________________"<<endl;
cout<<"1.2D Plane"<<endl<<"2.3D Plane"<<endl<<"3.Exit"<<endl<<"Enter your
choice:";
cin>>ch; //reading choice
switch(ch) //switch block
{
case 1:cout<<endl<<"Enter x1,x2,y1 and y2 values:";
cin>>a>>b>>c>>d;
cout<<"Distance="<<d1.distance(a,b,c,d)<<"sq. units."<<endl;
break; //calling distance() having 4 arguments in 2D plane
case 2:cout<<endl<<"Enter x1,x2,y1,y2,z1 and z2 values:";
cin>>a>>b>>c>>d>>e>>f;
cout<<"Distance="<<d1.distance(a,b,c,d,e,f)<<"sq. units."<<endl;
break; //calling distance() having 6 arguments in 3D plane
Page 13 of 56
case 3:exit(0);
default:cout<<"Invalid choice"<<endl;
} //end of switch block
}while(1); //end of do while block
return(0);
} //end of main
Output:

Page 14 of 56
Program 4 : Write a C++ Program to create a class called Complex.
Implement the following operations by overloading the operators +, - and *. Read
and Display the complex number by overloading the operator >> and <<
respectively.
1. Complex C3=C1+C2
2. Complex C3=C1-C2
3. Complex C3=C1*C2
Where C1, C2 and C3 are objects of type Complex.
#Program
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
class Complex //class defination
{
private: float x,y; //data members
public : Complex operator+(Complex C) //+operator function
{
Complex B;
B.x = x + C.x; //adding two complex numbers
B.y = y + C.y;
return(B);
}
Complex operator-(Complex C) //-operator function
{
Complex B;
B.x = x - C.x; //subtracting two complex numbers
B.y = y - C.y;
return(B);
}
Complex operator*(Complex C) //*operator function
{
Complex B;
B.x = x*C.x - y*C.y; //multiplying two complex numbers
B.y = x*C.y + y*C.x;
return(B);
}
friend istream& operator>>(istream& din,Complex& C);
friend ostream& operator<<(ostream& dout,Complex& C);
};

istream& operator>>(istream& din,Complex& C)


{ //friend >> operator function to read complex number
return(din>>C.x>>C.y);
Page 15 of 56
}

ostream& operator<<(ostream& dout,Complex& C)


{ //friend << operator function to display complex number
if(C.y>0)
return(dout<<C.x<<"+i"<<C.y<<endl);
else
return(dout<<C.x<<"-i"<<fabs(C.y)<<endl);
}

int main()
{
cout<<"C++ Program to perform 'ADDITION','SUBTRACTION' and
'MULTIPLICATION' on complex numbers."<<endl;
Complex C1,C2,C3; //object declarartion
int c;
char ch;
do //do while block
{
cout<<"_________________________________"<<endl;

cout<<"1.Read"<<endl<<"2.Display"<<endl<<"3.Addition"<<endl<<"4.Subtracti
on"<<endl<<"5.Multiply"<<endl<<"Enter your choice:";
cin>>c; //reading choice from user
switch(c) //switch block
{
case 1:cout<<"_________________________________"<<endl;
cout<<"Enter values for Complex 1:";
cin>>C1; //read complex 1
cout<<"Enter values for Complex 2:";
cin>>C2; //read complex 2
break;
case 2:cout<<"_________________________________"<<endl;
cout<<"C1="<<C1; //display complex 1
cout<<"C2="<<C2; //display complex 2
break;
case 3:C3=C1+C2; //call + operator function
cout<<"Addition of C1 and C2:"<<C3;
break;
case 4:C3=C1-C2; //call – operator function
cout<<"Subtraction of C1 and C2:"<<C3;
break;
case 5:C3=C1*C2; //call * operator function
cout<<"Multiplication of C1 and C2:"<<C3;
Page 16 of 56
break;
default:cout<<"Invalid choice"<<endl;
} //end of switch block
cout<<"Do you want to continue...(y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y'); //end of do while block
return(0);
} //end of main

Output:

Page 17 of 56
Program 5 :Write a C++ program to create a class called STRING and
implement the following operations by writing an appropriate constructor, an
overloaded operator ‘+’ and ‘>=’ and illustrate the use of friend function in
operator overloading.
1. STRING S1 = “SIT”
2. STRING S2 = ”TUMAKURU”
3. STRING S3 = S1+S2
4. Read the values for strings during run-time.
5. Compare any two strings in terms of length by overloading the operator
“>=”.
#Program
#include<iostream>
#include<string.h>
using namespace std;
class str
{
char *p;
int len;
public:
str() {
p=0;
len=0;
}
void operator = (const char *x);
friend str operator + (str q,str t); void

Page 18 of 56
void operator >=( str s);
friend ostream & operator << (ostream &sout,str q);
};
Str s1,s2,s3;
void str :: operator = (const char *x)
{
len=strlen(x);
p=new char(len+1);
strcpy(p,x);
}
str operator + (str q,str t)
{
Str temp;
temp.len=q.len+t.len;
temp.p=new char(temp.len+1);
strcpy(temp.p,q.p);
strcat(temp.p,t.p);
return(temp);
}
void str::operator >= (str s)
{
if(len<s.len)
cout<<"string1 < string2\n";
else
cout<<"string3 is > string2\n";
}
ostream & operator<<(ostream &sout,str q)
{
sout<<q.p;
return(sout);
}
int main() {
s1="VTU";
s2="SIT";
s3=s1+s2;
cout<<"string1 = "<<s1<<"\n"<<"string2 = "<<s2<<"\n"<<"string3 = "<<s3<<"\n";
s3>=s2; return 0;
}

Output:

Page 19 of 56
Program 6 : Create a class called OCTAL and implement the following
operations by writing an appropriate constructor, an overloaded operator ‘+’ and
illustrate the use of friend function in operator overloading. The program should
ask the user to enter octal to integer conversion and vice versa. Based on the
choice, add the values. Also display the values of h and y.
1. OCTAL h = x where x is an integer
2. int y = h+k where h is an OCTAL object and k is an integer
3. int y = k+h where k is an integer and h is OCTAL object.
#Program
#include<iostream>
#include<iomanip>
#include<math.h>
using namespace std;
class OCTAL //class defination
{
private: int oc,de; //data members
public : OCTAL() //default constructor
{}
OCTAL(int x) //parameterized constructor
{
de=x;
oc=0;
int i=0;
while(x!=0) //decimal to octal conversion
{
oc=oc+((x%8)*pow(10,i));
Page 20 of 56
x=x/8;
i++;
}
}
void octaltodecimal(int x) //member function to convert octal to decimal
{
oc=x;
de=0;
int i=0;
while(x!=0)
{
de=de+((x%10)*pow(8,i));
x=x/10;
i++;
}
}
void display() //display octal value
{
cout<<oc<<endl;
}
void displayd() //display decimal value
{
cout<<de<<endl;
}
void operator+(int r) // + operator function to add octal with decimal
{
cout<<"The sum is "<<(r+this->de)<<endl;
}
friend void operator+(int k,OCTAL &h);
};

void operator+(int k,OCTAL &h) // + operator function to add decimal with octal
{
cout<<"The sum is "<<(k+h.de)<<endl;
}

int main()
{
cout<<"C++ Program to convert octal to decimal and decimal to octal and adding an
integer to an object."<<endl;
OCTAL O; //object declaration
int c,x;
char ch;
do //do while block
Page 21 of 56
{
cout<<"__________________________"<<endl;
cout<<"1.Decimal to Octal"<<endl<<"2.Octal to
Decimal"<<endl<<"3.Octal+Decimal"<<endl<<"4.Decimal+Octal"<<endl<<"Enter your
choice:";
cin>>c; //read choice from the user
switch(c) //switch block
{
case 1:cout<<"__________________________"<<endl;
cout<<"Enter decimal value:";
cin>>x; //read decimal value
O=x; //convert to octal value
cout<<"The Octal value is ";
O.display(); //display octal value
break;
case 2:cout<<"__________________________"<<endl;
cout<<"Enter Octal value:";
cin>>x; //read octal value
O.octaltodecimal(x); //convert to decimal value
cout<<"The Decimal value is";
O.displayd(); //display decimal value
break;
case 3:cout<<"__________________________"<<endl;
cout<<"Enter an integer to add:";
cin>>x; //read an integer
O+x; //octal + integer
break;
case 4:cout<<"__________________________"<<endl;
cout<<"Enter an integer to add:";
cin>>x; //read an integer
x+O; //integer + octal
break;
default:cout<<"Invalid choice"<<endl;
} //end of switch block
cout<<"Do you want to continue...(y/n):";
cin>>ch;
}while(ch=='y'||ch=='Y'); //end of do while block
return(0);
} //end of main

Output:

Page 22 of 56
Page 23 of 56
Program 7 : Write a C++ program to create a class called VOLUME to compute
the volume of regular objects like Cube (a3), Cuboids (l*b*h), Sphere(4/3 pie r3)
Page 24 of 56
and Cone (1/3 pie r2h). Declare the appropriate data members; overload the
constructors to initialize the data members suitably. Write a friend function
called compute( ) to find the volumes of objects. Also keep track of number of
object created and destroyed using static data members.
#Program
#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<math.h>
using namespace std;
class Volume //class defination
{
private: float a,l,b,h,r,val; //data members
static int c;
public : Volume() //default constructor
{
c++;
cout<<"Object created"<<endl;
}
Volume(float i) //parameterized constructor
{
a=r=i;
c++;
cout<<"Object created"<<endl;
}
Volume(float i,float j) //parameterized constructor
{
r=i;
h=j;
c++;
cout<<"Object created"<<endl;
}
Volume(float i,float j,float k) //parameterized constructor
{
l=i;
b=j;
h=k;
c++;
cout<<"Object created"<<endl;
}
~Volume() //destructor
{
cout<<"Object deleted"<<endl;
c--;
Page 25 of 56
}
void display() //member function to display the volume
{
cout<<"Volume="<<val<<"cubic units."<<endl;
}
friend void compute(Volume &V,int ch);
};

void compute(Volume &V,int ch)


{ //friend function to find the volume of geometric figures
switch(ch)
{
case 1:V.val = V.a*V.a*V.a; // volume of cube
break;
case 2:V.val = V.l*V.b*V.h; //volume of cuboid
break;
case 3:V.val = (4*V.r*V.r*V.r*3.142)/3; //volume of sphere
break;
case 4:V.val = (V.h*V.r*V.r*3.142)/3; //volume of cone
break;
}
}

int Volume::c=0; //static data member intialization

int main()
{
cout<<"C++ Program to find the volumes of the geometric figures."<<endl;
int ch,l,b,h,r;
do //do while block
{
cout<<"_________________________"<<endl;

cout<<"1.Cube"<<endl<<"2.Cuboid"<<endl<<"3.Sphere"<<endl<<"4.Cone"<<en
dl<<"5.Exit"<<endl<<"Enter your choice:";
cin>>ch; //read choice from the user
switch(ch) //switch block
{
case 1:{
cout<<endl<<"Enter the value of cube:";
cin>>l;
Volume cube(l); //creating the object cube
compute(cube,ch); //computing the volume of cube
cube.display(); //display the volume of the cube
Page 26 of 56
}
break;
case 2:{
cout<<endl<<"Enter the length,breadth and height of cuboid:";
cin>>l>>b>>h;
Volume cuboid(l,b,h); //creating the object cuboid
compute(cuboid,ch); //computing the volume of cuboid
cuboid.display(); //display the volume of the cuboid
}
break;
case 3:{
cout<<endl<<"Enter the radius of sphere:";
cin>>r;
Volume sphere(r); //creating the object sphere
compute(sphere,ch); //computing the volume of sphere
sphere.display(); //display the volume of the sphere
}
break;
case 4:{
cout<<endl<<"Enter the radius and height of cone:";
cin>>r>>h;
Volume cone(r,h); //creating the object cone
compute(cone,ch); //computing the volume of cone
cone.display(); //display the volume of the cone
}
break;
case 5:exit(1);
default:cout<<"Invalid choice."<<endl;
} //end of switch block
}while(1); //end of do while block
return(0);
} //end of main

Output:

Page 27 of 56
Page 28 of 56
Page 29 of 56
8. Write a C++ program to do student mark analysis using static data member, default argument
and friend function for class. Define a class student with data members USN, total, m1, m2, m3,
name, grade, avg and static variable count. Member functions: to read data, to calculate average
and grade and to display the data. Define one more class with name personal with data members
address and mobile number and member functions to read data and to display. Include the
following specification to the program,
 Define function by name grade_point (to raise flag for pass) with default value 50.
 Define printline function (to print a line) with default value 70 for range and ‘*’for line.
 Make display function as friend for both classes.
 Use static members to keep a count of passed students.

#include<iostream>
#include<stdlib.h>
using namespace std;
int grade_point(int,int,int,int=50);
void printline(char ch='*',int range= 70);
class personal;
class student
{
int rno,tot;
static int count;
int m1,m2,m3; char name[20],grade;
float avg;
public:
void getdata()
{
cout<<"Enter id:"<<endl;
cin>>rno;
cout<<"Enter name:"<<endl; cin>>name;
cout<<"Enter m1"<<endl;
cin>>m1; cout<<"Enter
m2"<<endl; cin>>m2;
cout<<"Enter m3"<<endl;
cin>>m3;
}
friend void display(student,personal);
void cal()
{
tot=0;
if(cp(m1,m2,m3))
{
cout<<"Result=pass"<<endl;
tot=m1+m2+m3; avg=tot/3;
count=count+1; if(avg>=90)
grade='O';
if(avg>=70&&avg<90)
grade='A';
if(avg>=50&&avg<70)
grade='B';
}
else
{
Page 30 of 56
cout<<"Result=Fail"<<endl;
}
}
static void showcount(void)
{
cout<<endl;
cout<<"count:"<<count<<endl;
}
};
Int student::count;
class personal {
char add[25];
long int tel;
public:
void getdata1()
{
cout<<"Enter address"<<endl;
cin>>add;
cout<<"Enter phone number:"<<endl;
cin>>tel;
}
friend void display(student a,personal p){
cout<<endl;
cout<<"Name:"<<a.name<<endl;
cout<<"ID:"<<a.rno<<endl;
cout<<"Address:"<<p.add<<endl;
cout<<"Phone no.:"<<p.tel<<endl;
cout<<"M1:"<<a.m1<<endl;
cout<<"M2:"<<a.m2<<endl;
cout<<"M3:"<<a.m3<<endl;
if(cp(a.m1,a.m2,a.m3))
{
cout<<"Total:"<<a.tot<<endl;
cout<<"Average:"<<a.avg<<endl;
cout<<"Grade:"<<a.grade<<endl;
}
} };
void printline(char ch,int range)
{
int i;
cout<<endl;
for(i=0;i<range;i++)
cout<<ch; }
int cp(int m1,int m2,int m3,int pass1)
{
if(m1>=pass1&&m2>=pass1&&m3>=pass1)
return(1);
else
return(0);
}
int main() {
int i,n; per p1[20]; acc

Page 31 of 56
a1[20]; cout<<"Enter no of
students:"; cin>>n;
for(i=0;i<n;i++)
{
a1[i].getdata();
p1[i].getdata1();
a1[i].cal();
}
for(i=0;i<n;i++)
{
printline('-');
display(a1[i],p1[i]);
printline();
}
student::showcount();
return 0;
}

9. Write a C++ program to create a class called STUDENT with data members USN, Name and
Age. Using Inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as
Semester, Fees and Stipend. Enter the data for at least 5 students. Find the semester wise
average age for all UG and PG students separately.

#include<stdlib.h>
#include<iostream>

using namespace std;

class student
{
char name[25];
char usn[10];

protected :
int age;
public :
void get_data(void)
{
cout<<"enter the NAME, USN and AGE :";
cin>>name>>usn;
cin>>age;
}
void display()
{
cout<<name<<"\t\t"<<usn<<"\t\t"<<age<<"\t\t";
}
};

class ug : public student


{

float fees,stipend;

Page 32 of 56
protected :
int sem;
public :
void get_data(void)
{
student :: get_data();

cout<<"enter the semister,fee and stipend :";


cin>>sem;
if(sem>9)
{
Cout<<”Enter sem within 8”<<endl;
Cin>>sem;
}
cin>>fees>>stipend;
}
void show_data(void)
{
student :: display();
cout<<sem<<"\t\t"<<fees<<"\t\t"<<stipend<<endl;
}
friend void average_age( ug a[] ,int n);
};

void average_age(ug a[],int n)


{
int aver=0;
for(int i=0;i<8;i++)
{
int no=0;
aver=0;
for(int j=0;j<n;j++)
{
if(a[j].sem == i)
{
no++;
aver+=a[j].age;
}
}
if(aver !=0)
{
aver=aver/no;
cout<<i<<" UG Sem students average = "<<aver<<endl;
}
}
}

class pg : public student


{

float fees,stipend;
protected :

Page 33 of 56
int sem;
public :
void get_data(void)
{
student :: get_data();

cout<<"enter the semister, fee and stipend :";


cin>>sem;
if(sem>5)
{
Cout<<”Enter sem within 8”<<endl;
Cin>>sem;
}

cin>>fees>>stipend;
}
void show_data(void)
{
student :: display();
cout<<sem<<"\t\t"<<fees<<"\t\t"<<stipend<<endl;
}

friend void average_age( pg a[] ,int n);

};

void average_age(pg a[],int n)


{

int aver=0;
for(int i=0;i<8;i++)
{
int no=0;
aver=0;
for(int j=0;j<n;j++)
{
if(a[j].sem == i)
{
no++;
aver+=a[j].age;
}
}
if(aver !=0)
{
aver=aver/no;
cout<<i<<" Sem PG students average = "<<aver<<endl;
}

Page 34 of 56
int main()
{
int n1,n2;

cout<<"enter the no. of UG students :";


cin>> n1;
cout<<"enter the no of PG students :";
cin>>n2;

ug a[n1];
pg b[n2];

cout<<"\nenter the details of "<<n1<<" UG students\n"<<endl;


for(int i=0 ;i<n1;i++)
{
a[i].get_data();
}

cout<<"\nenter the details of "<<n2<<" PG students\n"<<endl;


for(int i=0 ;i<n2;i++)
{
b[i].get_data();
cout<<"\n";
}

cout<<"\n\nUG students DETAILS "<<endl;


cout<<"\nNAME\t\t USN\t\tAGE\t\tSEM\t\tFEES\t\tSTIPEND"<<endl;
cout<<"---------------------------------------------------------------------------------------------"<<endl ;
for(int i=0;i<n1;i++)
{
a[i].show_data();
cout<<endl;
}
average_age(a,n1);

cout<<"\n\nPG students DETAILS "<<endl;


cout<<"\nNAME\t\t USN\t\tAGE\t\tSEM\t\tFEES\t\tSTIPEND"<<endl;
cout<<"---------------------------------------------------------------------------------------------"<<endl;
for(int i=0;i<n2;i++)
{
b[i].show_data();
cout<<endl;
}
average_age(b,n2);

return 0;
}

Page 35 of 56
enter the no. of UG students :enter the no of PG students :
enter the details of 2 UG students

enter the NAME, USN and AGE :enter the semister,fee and stipend :enter the NAME, USN and AGE :enter the semister,fee and
stipend :
enter the details of 2 PG students

enter the NAME, USN and AGE :enter the semister, fee and stipend :
enter the NAME, USN and AGE :enter the semister, fee and stipend :

UG students DETAILS

NAME USN AGE SEM FEES STIPEND


--------------------------------------------------------------------------------------------- --------------------------------------------------
ss 123 23 2 200000 2222

sd 124 23 2 12112 21212

2 UG Sem students average = 23

PG students DETAILS

NAME USN AGE SEM FEES STIPEND


--------------------------------------------------------------------------------------------- --------------------------------------------------
dfgg 125 20 2 1213 1233

gjhdj 126 25 3 455 212

2 Sem PG students average = 20


3 Sem PG students average = 25

10. Consider a class diagram below. The class Master derives information from both Account and Admin
classes which in turn derive information from the class person. Define all the four classes and write a
program to create, update and display the information contained in Master objects. And also print
appropriate messages if record is not present.

Person

Name, code

Account Admin

Pay Experience

Master

Name, code,
Experience, Pay

Page 36 of 56
#include<iostream>
#include<cstdlib>
#include<string.h>
#include<iomanip>
using namespace std;
int count=0;
class person
{
protected:
char name[20];
int code;
public:
void getcode()
{
cout<<"\nEnter the code"<<endl;
cin>>code;

}
int rcode()
{ return code; }
//friend void update();
void getname()
{
cout<<"\n Enter the name"<<endl;
cin>>name;
}
};
class account : virtual public person
{
protected:
int pay;
public:
void getpay()
{
cout<<pay<<"\n";
cout<<"\n Enter payment"<<endl;
cin>>pay;
}
};
class admin:virtual public person
{
protected:
int exp;
public:
void getexp()
{
cout<<"Enter the experience"<<endl;
cin>>exp;
}
};
class master:public account,public admin
{

Page 37 of 56
public:
void create()
{
getcode();
getname();
getpay();
getexp();
}
//friend void update();
void display()
{ char ch;
cout<<"\n";
cout<<code<<setw(20);
cout<<name<<setw(10);
cout<<pay<<setw(17);
cout<<exp<<setw(03);
cout<<"\n";
}
};
master m[20];
void update()
{
int c,ch,i;
cout<<"enter code";
cin>>c;
for(i=0;i<count;i++)
{
if(c==m[i].rcode())
break;
}
if(i>count)
{
cout<<"code not found";
return;
}
cout<<"enter your choice"<<endl;
cout<<"1.payment 2.experience 3.exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:m[i].getpay();
break;
case 2:m[i].getexp();
break;
case 3:return;
default:cout<<"invalid entry";
}
}
int main()
{
int ch;
//master m[20];

Page 38 of 56
while(1)
{
cout<<"Enter your choice";
cout<<"1.create\n 2.update\n 3.display\n 4.exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:m[count++].create();
break;
case 2:
if(count==0)
{
Cout<<”Record not found”<<endl;
}
Else{
update();
}
break;
case 3:
if(count==0)
{
Cout<<”Record not found”<<endl;
}
Else{
cout<<"\nCODE"<<setw(20)<<"NAME"<<setw(10)<<"PAYMENT"<<setw(17)<<"EXPERIENCE"<<setw
(03);
for(int i=0;i<count;i++)
{
m[i].display();
}}
break;
case 4:exit(0);
default: cout<<"invalid entries";
}
}
return 0;
}
1
123
sd
20000
2
3
2
123
2
4
3
4
Enter your choice1.create
2.update
3.display
4.exit

Page 39 of 56
1
Enter the code123

Enter the name


ssdds

Enter payment 20000


Enter the experience 2
Enter your choice1.create
2.update
3.display
4.exit
3
CODE NAME PAYMENT EXPERIENCE
123 sd 20000 2
Enter your choice1.create
2.update
3.display
4.exit
enter code123 enter your choice 2
1.payment 2.experience 3.exit 2
Enter the experience4
Enter your choice1.create
2.update
3.display
4.exit

CODE NAME PAYMENT EXPERIENCE


123 sd 20000 4
Enter your choice1.create
2.update
3.display
4.exit
4
11. Write a C++ Program to create a template class for QUEUE and demonstrate its basic
operations.
#include<iostream>
#include<cstdlib>
using namespace std;
template<class t>
class queue

{
t s[10];
int rear,front,n;
public:
queue()
{
rear=0;
front=0;
n=0;
}
void insert(t ele)
{
Page 40 of 56
if(((rear+1)%10)!=front)
{
rear=(rear+1)%10;
s[rear]=ele;
n++;
}
else
{
cout<<"queue is full\n";
}
}
void remove()
{
if(n==0)
{
cout<<"queue is empty\n";
return;
}
else
{
cout<<"\n removed element is "<<s[front+1];
front=(front+1)%10;
n--;
}
}
void que_operation();
void display();
};
template<class t>
void queue <t>::display()
{
if(n>0)
{
cout<<"\nqueue content is :\n";
for(int i=(front+1)%10;;i=(i+1)%10)
{

cout<<s[i]<<"\t";
if(i==rear)
break;

}
}
else
Page 41 of 56
cout<<"\nqueue is emptyd\n";
}
template<class t>
void queue <t>::que_operation()
{
int choice,i;
t ele;
while(1)
{
cout<<"\n1.insert\n2.delete\n3.exit\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"\nenter the elemet to be inserted\n";
cin>>ele;
insert(ele);
display();
break;
case 2:remove();
display();
break;
case 3:return;
default:cout<<"invalid input\n";
}
}
}
int main()
{
cout<<"queue operation using template\n";
cout<<"integer operation\n";
queue<int>que1;
cout<<"float operation\n";
queue<float>que2;
int ch;
while(1)
{
cout<<"1.int queue\n2.float queue\n3.exit\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:que1.que_operation();
break;
case 2:que2.que_operation();
Page 42 of 56
break;

case 3:exit(0);
default:cout<<"invalid input\n";
}
}
return 0;
}
OUTPUT:
queue operation using template
integer operation
float operation
1.int queue
2.float queue
3.exit
enter your choice
1
1.insert
2.delete
3.exit
2
queue is empty
queue is emptyd
1.insert
2.delete
3.exit
1
enter the elemet to be inserted
10
queue content is :
10
1.insert
2.delete
3.exit
1
enter the elemet to be inserted
20
queue content is :
10 20
1.insert
2.delete
3.exit
1
enter the elemet to be inserted
Page 43 of 56
30

queue content is :
10 20 30
1.insert
2.delete
3.exit
1
enter the elemet to be inserted
40
queue content is :
10 20 30 40
1.insert
2.delete
3.exit
1
enter the elemet to be inserted
50
queue content is :
10 20 30 40 50
1.insert
2.delete
3.exit
1
enter the elemet to be inserted
60
queue content is :
10 20 30 40 50 60
1.insert
2.delete
3.exit
1
enter the elemet to be inserted
70
queue content is :
10 20 30 40 50 60 70
1.insert
2.delete
3.exit
2
removed element is 10
queue content is :
20 30 40 50 60 70
1.insert
Page 44 of 56
2.delete

3.exit
2
removed element is 20
queue content is :
30 40 50 60 70
1.insert
2.delete
3.exit
2
removed element is 30
queue content is :
40 50 60 70
1.insert
2.delete
3.exit
2
removed element is 40
queue content is :
50 60 70
1.insert
2.delete
3.exit
2
removed element is 50
queue content is :
60 70
1.insert
2.delete
3.exit
2
removed element is 60
queue content is :
70
1.insert
2.delete
3.exit
2
removed element is 70
queue is emptyd
1.insert
2.delete
3.exit
Page 45 of 56
3
1.int queue
2.float queue

3.exit
enter your choice
3
12. Write a C++ program to perform linear search using function templates.
#include<iostream>
#include<cstdlib>
using namespace std;
template<class t>
void lsearch(t a[],int m,t key)
{
int i,f=0;
for(i=0;i<m;i++)
{
if(a[i]==key)
{
f=1;
break;
}
}
if(f==1)
{
cout<<"Key is found\n";
cout<<"At pos:\n"<<"|"<<i+1<<"|"<<"\n";
}
else
{
cout<<"Key is not found\n";
}
}
template<class t1>
void display(t1 a[],int m)
{
for(int i=0;i<m;i++)
cout<<a[i]<<"\t";
}
int main()
{
char z[20];
int x[20];
float y[20];
Page 46 of 56
int m;
int ch;
while(1)
{
cout<<"enter 1.search int array\n";
cout<<"enter 2.search float array\n";

cout<<"enter 3.search char array\n";


cout<<"enter 4.for exit\n";
cin>>ch;
if(ch==1)
{
cout<<"Enter int array size\n";
cin>>m;
cout<<"Enter elements\n";
for(int i=0;i<m;i++)
{
cin>>x[i];
}
cout<<"array elements are\n";
display(x,m);
int key1;
cout<<"enter key\n";
cin>>key1;
lsearch(x,m,key1);
}
else if(ch==2)
{
cout<<"Enter float array size\n";
int m,i;
cin>>m;
cout<<"Enter elements\n";
for(i=0;i<m;i++)
{
cin>>y[i];
}
cout<<"array elements are\n";
display(y,m);
float key2;
cout<<"enter key\n";
cin>>key2;
lsearch(y,m,key2);
}
else if(ch==3)
Page 47 of 56
{
cout<<"Enter array size\n";
int m,i;
cin>>m;
for(i=0;i<m;i++)
{
cin>>z[i];
}
cout<<"array elements are\n";
display(z,m);
char key4;
cout<<"enter key\n";
cin>>key4;
lsearch(z,m,key4);
}

else
{
return(0);
}
}
return(0);
}
OUTPUT:
enter 1.search int array
enter 2.search float array
enter 3.search char array
enter 4.for exit
1
Enter int array size
5
Enter elements
1
5
8
7
9
array elements are
1 5 8 7 9 enter key
9
Key is found
At pos:
|5|
enter 1.search int array
Page 48 of 56
enter 2.search float array
enter 3.search char array
enter 4.for exit
1
Enter int array size
5
Enter elements
1
5
8
7

9
array elements are
1 5 8 7 9 enter key
0.
Key is not found
enter 1.search int array
enter 2.search float array
enter 3.search char array
enter 4.for exit
ise@isevccpc20:~/oops_lab$ 3
3: command not found
ise@isevccpc20:~/oops_lab$ ./a.out
enter 1.search int array
enter 2.search float array
enter 3.search char array
enter 4.for exit
3
Enter array size
6
a
b
v
f
g
r
array elements are
a b v f g r enter key
u
Key is not found
enter 1.search int array
enter 2.search float array
enter 3.search char array
Page 49 of 56
enter 4.for exit
2
Enter float array size
3
Enter elements

1.1
8.8
9.4
array elements are
1.1 8.8 9.4 enter key
1.1
Key is found
At pos:
|1|
enter 1.search int array
enter 2.search float array
enter 3.search char array
enter 4.for exit
4
13.Create a base class called SHAPE. Derive three specific classes called triangle, rectangle and circle
from base shape. Write C++ program to initialize base class data members, to compute and display
the area of figures. Make member function as a virtual function and redefine this function in the
derived classes to suit their requirements.
//shape using virtual function
#include<iostream>
#include<cstdlib>
using namespace std;
class shape
{
public:double x,y;
virtual void getdata(double p,double q=0)
{}
virtual void displayarea(){}
};
class tri:public shape
{
public:void getdata(double p,double q=0)
{
x=p;
y=q;
}
void displayarea()
{
double area=0.5*x*y;
cout<<"area of triangle is:"<<area<<"\n";
}
};
class rect:public shape
Page 50 of 56
{
public:
void getdata(double p,double q=0)
{
x=p;
y=q;
}
void displayarea()
{
double area=x*y;
cout<<"area of rectangle is:"<<area<<"\n";
}
};
class circle:public shape
{
public:
void getdata(double p,double q=0)
{
x=p;
y=q;
}
void displayarea()
{
double area=3.1405*x*x;
cout<<"area of circle is:"<<area<<"\n";
}
};
int main()
{
double var1,var2;
int ch;
shape *sp;
sp=new shape;
tri t;
rect r;
circle c;
while(1)
{
cout<<"area of:\n";
cout<<"1.triangle\n2.rectangle\n3.circle\n4.exit\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter base and height\n";
cin>>var1>>var2;
sp=&t;
sp->getdata(var1,var2);
sp->displayarea();
break;
case 2:cout<<"enter length and breadth\n";
cin>>var1>>var2;
sp=&r;

Page 51 of 56
sp->getdata(var1,var2);
sp->displayarea();
break;
case 3:cout<<"enter radius\n";
cin>>var1;
sp=&c;
sp->getdata(var1);
sp->displayarea();
break;
case 4:exit(0);
default:cout<<"invalid input\n";
}
}
return 0;
}

area of:
1.triangle
2.rectangle
3.circle
4.exit
enter base and height
area of triangle is:2
area of:
1.triangle
2.rectangle
3.circle
4.exit
enter length and breadth
area of rectangle is:4
area of:
1.triangle
2.rectangle
3.circle
4.exit
enter radius
area of circle is:12.562
area of:
1.triangle
2.rectangle
3.circle
4.exit

14. Write a C++ program to create a class called STACK and perform its basic operation. Use try,
catch block to handle the stack empty and stack full exception.
#include<cstdlib>
#include<iostream>

#inlcude<iomanip>
using namespace std;
class stack
{
public:
int s[5];
int t;
stack()
{
t=-1;
}
void push()
Page 52 of 56
{
try
{
if(t==4)
throw(1);
else
cout<<"Enter Item"<<endl;
int item;
cin>>item;
t+=1;
s[t]=item;
}
catch(int i)
{
if(i==1)
cout<<"Overflow"<<endl;
}
}
void pop()
{
try
{
if(t==-1)
throw(2);
else
cout<<"Poped item:"<<setw(10)<<s[t--];
}
catch(int i)
{
if(i==2)
cout<<"Underflow"<<endl;
}
}
void display()
{

cout<<"Elements are:\n";
if(t!=-1)
{
for(int i=0;i<=t;i++)
{
cout<<s[i]<<setw(10);
// r++;
}
}
else
{
cout<<"Empty stack!!!!!"<<endl;
// return(0);
}
}
};
int main()
{
stack st;
while(1)
{
cout<<"Enter\n 1:Push\n 2:Pop\n 3:Display\n 4:exit"<<endl;
int ch;
cin>>ch;
if(ch==1)
{
st.push();
Page 53 of 56
}
else
if(ch==2)
{
st.pop();
}
else
if(ch==3)
{
st.display();
}
else
if(ch==4)
{
exit(0);
}
}
return(0);
}
OUTPUT:
Enter
1:Push

2:Pop
3:Display
4:exit
2
Underflow
Enter
1:Push
2:Pop
3:Display
4:exit
3
Elements are:
Empty stack!!!!!
Enter
1:Push
2:Pop
3:Display
4:exit
1
Enter Item
10
Enter
1:Push
2:Pop
3:Display
4:exit
1
Enter Item
20
Enter
1:Push
2:Pop
3:Display
4:exit
1
Enter Item
30
Enter
1:Push
2:Pop
Page 54 of 56
3:Display
4:exit
1
Enter Item
40
Enter
1:Push
2:Pop
3:Display
4:exit
1
Enter Item

50
Enter
1:Push
2:Pop
3:Display
4:exit
1
Overflow
Enter
1:Push
2:Pop
3:Display
4:exit
3
Elements are:
10 20 30 40 50 Enter
1:Push
2:Pop
3:Display
4:exit
2
Poped item: 50Enter
1:Push
2:Pop
3:Display
4:exit
2
Poped item: 40Enter
1:Push
2:Pop
3:Display
4:exit
2
Poped item: 30Enter
1:Push
2:Pop
3:Display
4:exit
3
Elements are:
10 20 Enter
1:Push
2:Pop
3:Display
4:exit
2
Poped item: 20Enter
1:Push
2:Pop
3:Display
Page 55 of 56
4:exit
2

Poped item: 10Enter


1:Push
2:Pop
3:Display
4:exit
2
Underflow
Enter
1:Push
2:Pop
3:Display
4:exit
3
Elements are:
Empty stack!!!!!
Enter
1:Push
2:Pop
3:Display
4:exit
4
****************************************************************************

Page 56 of 56

Вам также может понравиться