Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

APSE LAB

Submitted by:
Name:
Surabhi Agarwal
Roll no.:
1130294
Section: E5
INDEX

 Write a C++ program to implement structures of class and member

function by reading and printing the student roll no. , name and also write

the function to assign the grade of student.


 C++ program to demonstrate the working of friend function.
 C++ program to multiply two complex numbers using operator

overloading.
 C++ program to implement THIS pointer
 C++ program to implement multiple and multilevel inheritance.
 C++ program to achieve hybrid inheritance.
 C++ program to achieve late binding through the virtual functions.
 C++ program to apply the concept of inheritance to estimate the bills of

various consumers subject to two part tariff.


 C++ program to simulate game of chances using OOP paradigm.
 C++ program to implement function templates to find out the roots of

second order quadratic equation

Program 1:
Write a c++ program to implement structures of class and member
function by reading and printing the student roll no. , name and also write
the function to assign the grade of student.
#include<iostream>
using namespace std;
class student
{
char name[30];
int marks;
public:
void getdata();
void show();
void grade();
};
void student:: getdata()
{
cout<<"\nEnter name and marks\n";
cin>>name>>marks;
}
void student::show()
{
cout<<"\nName: "<<name<<endl;
cout<<"Marks: "<<marks<<endl;
}
void student::grade()
{
int m=marks;
if(m>=85 && m<=100)
cout<<"A+"<<endl;
else if(m>=75&&m<85)
cout<<"A"<<endl;
else if (m>=65 && m<75)
cout<<"B"<<endl;
else if(m>=50 && m<65)
cout<<"C"<<endl;
else if(m>=40 && m<50)
cout<<"D"<<endl;
else if(m<40)
cout<<"Fail"<<endl;
else if(m<0&&m>100)
cout<<"Invalid marks"<<endl;
}
int main ()
{
student s;
s.getdata();
s.show();
s.grade();
return 0;
}

Program 2:
C++ program to demonstrate the working of friend function.
#include<iostream>
using namespace std;
class test
{
int a,b;
public:
void get(int x ,int y){a=x;b=y;}
friend float average(test);
};
float average(test t)
{
return float(t.a+t.b)/2;
}
int main()
{
test t;
t.get(4,5);
cout<<"average is:"<<average (t);
return 0;
}
Program 3:
C++ program to multiply two complex numbers using operator
overloading.
#include<iostream>
using namespace std;
class complex
{
float r,i;
public:
complex(){};
complex(float x, float y){r=x; i=y;}
complex operator +(complex);
complex operator *(complex);
void show();
};
complex complex :: operator +(complex c2)
{
complex c3;
c3.r=c2.r+r;
c3.i=c2.i+i;
return c3;
}
complex complex :: operator *(complex c2)
{
complex c3;
c3.r=(r*c2.r)-(i*c2.i);
c3.i=(r*c2.i)+(i*c2.i);
return c3;
}
void complex :: show()
{
if (i>=0)
cout <<r<<"+j"<<i<<endl;
else
cout<<r<<"-j"<<-1*i<<endl;
}
int main()
{
complex c1(5,7.5);
complex c2(5.6,7.9);
complex c3;
cout<<"addition:"<<endl;
c3=c1+c2;
c3.show();
cout<<"multiplication:"<<endl;
c3=c1*c2;
c3.show();
return 0;
}
Program 4:
C++ program to implement THIS pointer
#include<iostream>
#include<string.h>
using namespace std;
class person
{
char name[50];
int age;
public:
void getdata(int a, char*s);
person greater(person);
void show();
};
void person :: getdata(int a, char*s)
{
strcpy(name,s);
age=a;
}
void person :: show()
{
cout<<"name:"<<name<<endl;
cout<<"age:"<<age<<endl;
}
person person :: greater(person x)
{
if (x.age> this-> age)
return x;
}

int main()
{
person p1, p2, p3;
p1.getdata(25,"abc");
p2.getdata(27,"xyz");
p3=p1.greater(p2);
p3.show();
return 0;
}
Program 5:
C++ program to implement multiple and multilevel inheritance.
#include<iostream>
using namespace std;
class student
{
char name[30];
int r;
public:
void getdata()
{
cout<<"enter name and roll no."<<endl;
cin>>name;
cin>>r;
}
void showdata()
{
cout<<"name:"<<name<<endl;
cout<<"roll no."<<r<<endl;
}
};
class test:virtual public student
{
protected:
int s1, s2;
public:
void getmarks()
{
cout<<"enter subject 1 and 2 marks"<<endl;
cin>>s1>>s2;
}
void showmarks()
{
cout<<"subject 1: "<<s1<<endl;
cout<<"subject 2: "<<s2<<endl;
}
};
class sports:virtual public student
{
protected:
int m;
public:
void gets_marks()
{
cout<<"enter sports marks"<<endl;
cin>>m;
}
void shows_marks()
{
cout<<"sports marks: "<<m<<endl;
}
};
class result: public test, public sports
{
int total;
public:
void display();
};
void result:: display()
{
getdata();
getmarks();
gets_marks();
showdata();
showmarks();
shows_marks();
total=s1+s2+m;
cout<<"total:"<<total<<endl;
}
int main()
{
result s;
s.display();
return 0;
}

Program 6:
C++ program to achieve hybrid inheritance.
#include <iostream>
using namespace std;

class Number
{
private:
int num;
public:
void getNumber(void)
{
cout << "Enter an integer number: ";
cin >> num;
}
//to return num
int returnNumber(void)
{ return num; }
};

//Base Class 1, to calculate square of a number


class Square:public Number
{
public:
int getSquare(void)
{
int num,sqr;
num=returnNumber(); //get number from class Number
sqr=num*num;
return sqr;
}
};

//Base Class 2, to calculate cube of a number


class Cube:public Number
{
private:

public:
int getCube(void)
{
int num,cube;
num=returnNumber(); //get number from class Number
cube=num*num*num;
return cube;
}
};
int main()
{
Square objS;
Cube objC;
int sqr,cube;

objS.getNumber();
sqr =objS.getSquare();
cout << "Square of "<< objS.returnNumber() << " is: " << sqr << endl;

objC.getNumber();
cube=objC.getCube();
cout << "Cube of "<< objS.returnNumber() << " is: " << cube << endl;

return 0;
}
Program 7:
C++ program to achieve late binding through the virtual functions.
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show()
{
std::cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
std::cout << "Derived Class";
}
};

int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Late Binding Ocuurs
}
Program 8:
C++ program to apply the concept of inheritance to estimate the bills of
various consumers subject to two part tariff.
using namespace std;
#include <iostream>
#include <conio.h>

class account
{
char cust_name[20];
int acc_no;
char acc_type[20];
public:
void get_accinfo()
{
cout<<"\n\nEnter Customer Name :- ";
cin>>cust_name;
cout<<"Enter Account Number :- ";
cin>>acc_no;
cout<<"Enter Account Type :- ";
cin>>acc_type;
}
void display_accinfo()
{
cout<<"\n\nCustomer Name :- "<<cust_name;
cout<<"\nAccount Number :- "<<acc_no;
cout<<"\nAccount Type :- "<<acc_type;
}
};

class cur_acct : public account


{
static float balance;
public:
void disp_currbal()
{
cout<<"\nBalance :- "<<balance;
}
void deposit_currbal()
{
float deposit;
cout<<"\nEnter amount to Deposit :- ";
cin>>deposit;
balance = balance + deposit;
}
void withdraw_currbal()
{
float penalty,withdraw;
cout<<"\n\nBalance :- "<<balance;
cout<<"\nEnter amount to be withdraw :-";
cin>>withdraw;
balance=balance-withdraw;
if(balance < 500)
{
penalty=(500-balance)/10;
balance=balance-penalty;
cout<<"\nBalance after deducting penalty : "<<balance;
}
else if(withdraw > balance)
{
cout<<"\n\nYou have to take permission for Bank Overdraft Facility\n";
balance=balance+withdraw;
}
else
cout<<"\nAfter Withdrawl your Balance revels : "<<balance;
}
};

class sav_acct : public account


{
static float savbal;
public:
void disp_savbal()
{
cout<<"\nBalance :- "<<savbal;
}
void deposit_savbal()
{
float deposit,interest;
cout<<"\nEnter amount to Deposit :- ";
cin>>deposit;
savbal = savbal + deposit;
interest=(savbal*2)/100;
savbal=savbal+interest;
}
void withdraw_savbal()
{
float withdraw;
cout<<"\nBalance :- "<<savbal;
cout<<"\nEnter amount to be withdraw :-";
cin>>withdraw;
savbal=savbal-withdraw;
if(withdraw > savbal)
{
cout<<"\n\nYou have to take permission for Bank Overdraft Facility\n";
savbal=savbal+withdraw;
}
else
cout<<"\nAfter Withdrawl your Balance revels : "<<savbal;
}
};

float cur_acct :: balance;


float sav_acct :: savbal;
int main()
{
cur_acct c1;
sav_acct s1;

cout<<"\nEnter S for saving customer and C for current a/c customer\n\n";


char type;
cin>>type;

int choice;

if(type=='s' || type=='S')
{
s1.get_accinfo();
while(1)
{
cout<<"\nChoose Your Choice\n";
cout<<"1) Deposit\n";
cout<<"2) Withdraw\n";
cout<<"3) Display Balance\n";
cout<<"4) Display with full Details\n";
cout<<"5) Exit\n";
cout<<"6) Choose Your choice:-";
cin>>choice;
switch(choice)
{
case 1 : s1.deposit_savbal();
getch();
break;
case 2 : s1.withdraw_savbal();
getch();
break;
case 3 : s1.disp_savbal();
getch();
break;
case 4 : s1.display_accinfo();
s1.disp_savbal();
getch();
break;
case 5 : goto end;
default: cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
else
{
{
c1.get_accinfo();
while(1)
{
cout<<"\nChoose Your Choice\n";
cout<<"1) Deposit\n";
cout<<"2) Withdraw\n";
cout<<"3) Display Balance\n";
cout<<"4) Display with full Details\n";
cout<<"5) Exit\n";
cout<<"6) Choose Your choice:-";
cin>>choice;
switch(choice)
{
case 1 : c1.deposit_currbal();
getch();
break;
case 2 : c1.withdraw_currbal();
getch();
break;
case 3 : c1.disp_currbal();
getch();
break;
case 4 : c1.display_accinfo();
c1.disp_currbal();
getch();
break;
case 5 : goto end;
default: cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
end:
return 0;
}}
Program 9:
C++ program to simulate game of chances using OOP paradigm
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int rand_num()
{
return ( 1 + (rand()%6) );
}
int main( void )
{
int D1, D2, score, point;
int D3, D4, score_n = 0;

srand ( time( NULL ) );

D1 = rand_num();
D2 = rand_num();

score = D1 + D2;
printf( "You rolled %d + %d = %d", D1, D2, score );

if ( score == 7 || score == 11 )
printf( "\n\nYou win!");

else if ( score == 2 || score == 3 || score == 12 )


printf( "\n\nYou lose!");

else
{
point = score;
printf( "\n\nYou must get %d to win", point);

do
{
D3 = rand_num();
D4 = rand_num();

score_n = D3 + D4;
printf( "\n\nYou rolled %d + %d = %d", D3, D4, score_n );
if ( score_n == 7 )
printf( "\n\nYou lose!" );

if ( score_n == point )
printf( "\n\nYou win!" );

}while ( score_n == point || score_n == 7 );

}
return 0;
}
Program 10:
C++ program to implement function templates to find out the roots of
second order quadratic equation.
using namespace std;
#include<iostream>
#include<math.h>

template<class T>
void roots(T a, T b , T c )
{float d;
d=pow(b,2)-4*a*c;
if(d==0)
{cout<<"\n\nroots are real and equal\n";
cout<<"Root is"<<(-1)*b/(2*a);
}
else if(d>0)
{cout<<"\n\nroots are real and distinct\n";
cout<<"Root 1 is\t"<<((-1)*b+sqrt(d))/(2*a);
cout<<"Root 2 is\t"<<((-1)*b-sqrt(d))/(2*a);
}
else
{cout<<"\n\nRoots are complex\n";
cout<<"Real part of root is\t"<<((-1)*b)/(2*a);
cout<<"\nImaginary part of root is\t"<<sqrt((-1)*d)/(2*a);
}
}

int main()
{roots(1,2,3);
roots(1.6,5.8,52.6);
return 0;
}

You might also like