0% found this document useful (0 votes)
5 views13 pages

Lab 6

This document describes an experiment on inheritance in C++. The objective is to learn about base and derived classes and different types of inheritance. It provides code examples to demonstrate multiple inheritance and creating a banking account inheritance hierarchy.

Uploaded by

UMAIR Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views13 pages

Lab 6

This document describes an experiment on inheritance in C++. The objective is to learn about base and derived classes and different types of inheritance. It provides code examples to demonstrate multiple inheritance and creating a banking account inheritance hierarchy.

Uploaded by

UMAIR Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

AIR UNIVERSITY

DEPARTMENT OF MECHATRONICS ENGINEERING

EXPERIMENT NO 6

Lab Title: Inheritance


Student Name:. Umair Sarwar . Reg. No:. 221728 .

Objective: In this lab we will learn:


□ Base and derived classes
□ Public
inheritance □
Private
inheritance
□ Protected inheritance.
LAB ASSESSMENT:

Attributes Excellent Good Average Satisfactory (2) Unsatisfactory (1)


(5) (4) (3)

Ability to Conduct
Experiment

Ability to assimilate the


results

Effective use of lab


equipment and
follows the lab safety
rules
Total Marks Obtained Marks:

LAB REPORT ASSESSMENT:

Attributes Excellent Good Average Satisfactory (2) Unsatisfactory (1)


(5) (4) (3)

Data presentation

Experimental results
Conclusion

Total Marks: Obtained Marks:


QUESTION NUMBER :01
By using multiple inheritance, make a student class
representing student with data member roll no and two
member functions get_roll_no to get and
display_roll_no to display roll number of student. A test
class (derived class of student) representing the
scores of the student in two subjects and sports
class(derive class of stu) representing the score in
sports. The sports and test class should be inherited
by a result class having the functionality to add the
scores of two subjects and sports score an then
display the final result for students.

CODE:
#include <iostream>
using namespace std;
class Student {
protected:
int roll_no;
public:
void get_roll_no() {
cout << "Enter roll number: ";
cin >> roll_no;
}
void display_roll_no() {
cout << "Roll number: " << roll_no << endl;
}
};
class Test : virtual public Student {
protected:
int score1, score2;
public:
void get_scores() {
cout << "Enter score 1: ";
cin >> score1;
cout << "Enter score 2: ";
cin >> score2;
}
void display_scores() {
cout << "Score 1: " << score1 << endl;
cout << "Score 2: " << score2 << endl;
}
};
class Sports : virtual public Student {
protected:
int sports_score;
public:
void get_sports_score() {
cout << "Enter sports score: ";
cin >> sports_score;
}
void display_sports_score() {
cout << "Sports score:"
<< sports_score << endl;
}
};
class Result : public Test, public Sports {
public:
void display_result() {
int total_score;
total_score = score1 + score2 + sports_score;
cout << "Total score: " << total_score << endl;
}
};
int main() {
Result r;
r.Student::get_roll_no();
r.Test::get_scores();
r.Sports::get_sports_score();
r.Student::display_roll_no();
r.Test::display_scores();
r.Sports::display_sports_score();
r.display_result();
return 0;
}

OUTPUT

QUESTION NUMBER 2:

Create an inheritance hierarchy that a bank might use to


represent customers' bank accounts. All customers each
having an account no. at this bank can deposit
(i.e., credit) money into their accounts and withdraw
(i.e., debit) money from their accounts. More specific
types of accounts also exist. CreditCardAccount, for
instance, provide the user the facility to make money
transactions using ATM the money they hold.
Checking accounts, on the other hand, charge a fee
per transaction (i.e., credit or debit).
Create an inheritance hierarchy containing base class
Account and derived classes CreditCardAccount
and CheckingAccount that inherit from class
Account. Base class Account should include one
data member of type double to represent the
account balance. Customer’s name and account no.
The account no. should be unique and assigned
in the order in which instances are created The
class should provide a constructor that receives
an initial balance and uses it to initialize the data
member. The constructor should validate the
initial balance to ensure that it is greater than or
equal to 0.0. If not, the balance should be set to
0.0 and the constructor should display an error
message, indicating that the initial balance was
invalid.The class should provide following
member functions.
° Member function credit should add an amount to the
current balance.
° Member function debit should withdraw money
from the Account and ensure that the debit
amount does not exceed the Account's
balance. If it does, the balance should be left
unchanged and the function should print the
message "Debit amount exceeded account
balance.
° Member function getBalance should

return the current balance. ° Member

function getAccountNo.

• Derived class CreditCardAccount should inherit


the functionality of an Account, but also include a
data memberpinnumberset by the customer.
° Constructor should receive the initial

balance, as well as pin number. ° It should

provide a public member function resetpin

• Derived class CheckingAccount should inherit


from base class Account and include an additional
data member of type double that represents the fee
charged per transaction to all the customers.
° CheckingAccount's constructor should receive
the initial balance, as well as a parameter
indicating a fee amount.
° Class CheckingAccount should redefine
member functions credit and debit so that they
subtract the fee from the account balance
whenever either transaction is performed
successfully. CheckingAccount's versions of
these functions should invoke the base-class
Account version to perform the updates to an
account balance.
CheckingAccount's debit function should
charge a fee only if money is actually
withdrawn (i.e., the debit amount does not
exceed the account balance). After defining
the classes in this hierarchy, write a
program that creates objects of each class
and tests their member functions.
Note: In all the classes make the member
functions & data members const ,static where
required.

CODE:
#include <iostream>
using namespace std;
class Account
{
protected:
double acc_bal;
string name;
int acc_no,n;
public:
Account(double bal,int no)
{
acc_no=no;
cout<<"Account no."<<acc_no<<endl;
acc_bal=bal;
if(bal<0)
{
acc_bal=0;
cout<<"Initial balance was invalid"<<endl;
}
}
void credit()
{
double cred;
cout<<"Enter the amount you wants to add"<<endl;
cin>>cred;
acc_bal+=cred;
}
void debit()
{
double debb;
cout<<"Enter the amount:";
cin>>debb;
if(debb<=acc_bal && debb>=0)
{
}
else
{
acc_bal-=debb;
n=1;
cout<<"Debit amount exceeded account balance"<<endl;
n=0;
}
}
void get_balance()
{
cout<<"CURRENT BALANCE:"<<acc_bal<<endl;
}
};
class CreditCardAccount:public Account
{
public:
int pin,p;
CreditCardAccount(int pinnumber,double bal,int
no):Account( bal,no)
{
pin=pinnumber;
}
void resetpin()
{
cout<<"ENTER NEW PIN NUMBER:";
cin>>p;
pin=p;
cout<<"Your pin number is reseted"<<endl;
}
};
class CheckingAccount:public CreditCardAccount
{
protected:
double fee;
public:
CheckingAccount(const int fee,double bal,int
pinnumber,int
no):CreditCardAccount(pinnumber,bal,no),fee(fee)
{
}
void credit()
{
Account::credit();
acc_bal-=fee;
cout<<"CURRENT BALANCE:"<<acc_bal<<endl;
}
void debit()
{
Account::debit();
if (n==1)
{
acc_bal-=fee;
cout<<"CURRENT BALANCE:"<<acc_bal<<endl;
}
}
};
int main()
{
int t,j;
CheckingAccount c2(700,456875,221728,00001);
cout<<"ENTER pinnumber:";
cin>>t;
do{
if(t==c2.pin)
{
cout<<"\n\tWELCOME\n"<<endl;
cout<<"1.Debit\n2.Credit\n3.Check Balance\n4.Reset
pin\n5.Menu"<<endl;
int s;
cout<<"ENTER serial no.";
cin>>s;
switch (s)
{
case 1:
c2.debit();
break;
case 2:
c2.credit();
break;
case 3:
c2.get_balance();
break;
case 4:
c2.resetpin();
break;
case 5:
j=5;
break;
default:
cout<<"Invalid!"<<endl;
}
cout<<"Enter 0 to go back to the menu and press any key to
exit"<<endl;
cin>>j;
}
}
while(j==0);
}
OUTPUT:

CONCLUTION:
In this lab we learn the use of inheritance in classes. We can learn about
public, private, and protected inheritance. also, we learn about constructor
and destructor and learn about methods to use constructor, destructor in
inheritance.

You might also like