Experiment: 11: Aim: To Study Inheritance in C++. Learning Outcomes: Learner Would Be Able To

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

SVKM’s NMIMS University

Mukesh Patel School of Technology Management & Engineering


COURSE: Programming for Problem Solving

Experiment: 11
PART A

(PART A: TO BE REFFERED BY STUDENTS)

Aim: To study inheritance in C++.

Learning Outcomes: Learner would be able to

Implement inheritance.

Task 1: Assume that a bank maintains two kinds of accounts for customers, one called as
savings and the other as current account. The savings account provides compound interest
and withdrawal facilities but no cheque book facility. The current account provides cheque
book facility but no interest. Current account holders should also maintain a minimum
balance and if the balance falls below this level a service charge is imposed. Create a class
account that stores customer name, account number and type of account. From this derive the
classes cur_acct and sav_acct to make them more specific to their requirements. Include
necessary member functions in order to achieve the following tasks:

(a) Accept the deposit from a customer and update the balance.

(b) Display the balance.

(c) Compute and deposit interest.

(d) Permit withdrawal and update the balance.

(e) Check for the minimum balance, impose penalty, necessary and update the balance.

Task 2: An educational institution wishes to maintain a database of its employees. The


database is divided into a number of classes whose hierarchical relationships are shown in
following figure. The figure also shows the minimum information required for each class.
Specify all classes and define functions to create the database and retrieve individual
information as and when required.
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

PART B

(PART B: TO BE COMPLETED BY STUDENTS)

Students must submit the soft copy as per following segments within two hours of
the practical. The soft copy must be uploaded on the portal at the end of the
practical. The filename should be PPS_batch_rollno_experimentno Example:
PPS_B2_B001_Exp1

Roll No.: R020 Name: KAUSHAL JAIN

Prog/Yr/Sem:MBA TECH AI/1ST YEAR/ Batch:B1


SEM 1ST

Date of Experiment: Date of Submission:

Task 1:

#include <iostream>

using namespace std;

class account

public:

string name;

long long accno;

void getdata()

cout << "Enter the name of the customer: ";

getline(cin >> ws, name);

cout << "Enter the account number of the customer: ";

cin >> accno;

void display()
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

cout << "The name of the customer is: " << name << endl;

cout << "The account number of the customer is: " << accno << endl;

};

class cur_acct : public account

public:

int balance;

void getdata()

account::getdata();

cout << "Enter the balance of the customer: ";

cin >> balance;

void display()

account::display();

cout << "The balance of the customer is: " << balance << endl;

cout << endl;

void deposit()

int dep;

cout << "Enter the amount to be deposited: ";

cin >> dep;


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

balance += dep;

void withdraw()

int wit;

cout << "Enter the amount to be withdrawn: ";

cin >> wit;

if (wit > balance)

cout << "Insufficient balance" << endl;

else

balance -= wit;

};

class sav_acct : public account

public:

int balance;

void getdata()

account::getdata();

cout << "Enter the balance of the customer: ";

cin >> balance;


SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

void display()

account::display();

cout << "The balance of the customer is: " << balance << endl;

cout << endl;

void deposit()

int dep;

cout << "Enter the amount to be deposited: ";

cin >> dep;

balance += dep;

void withdraw()

int wit;

cout << "Enter the amount to be withdrawn: ";

cin >> wit;

if (wit > balance)

cout << "Insufficient balance" << endl;

else

balance -= wit;
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

void interest()

int rate, time;

cout << "Enter the rate of interest: ";

cin >> rate;

cout << "Enter the time period: ";

cin >> time;

balance += (balance * rate * time) / 100;

};

int main()

cur_acct c;

sav_acct s;

int ch;

cout << "Enter\n(1) for current account\n(2) for savings account: ";

cin >> ch;

cout << endl;

if (ch == 1)

c.getdata();

c.deposit();

c.display();

c.withdraw();
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

c.display();

else if (ch == 2)

s.getdata();

s.deposit();

s.display();

s.withdraw();

s.display();

s.interest();

s.display();

else

cout << "Invalid choice" << endl;

cout << endl;

}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

Task 2:

#include <bits/stdc++.h>

using std::cout, std::cin, std::string, std::endl, std::getline, std::ws;

class employee

public:

string name;

int empno;

void getdata()

{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

cout << "Enter the name of the employee: ";

getline(cin >> ws, name);

cout << "Enter the employee number of the employee: ";

cin >> empno;

void display()

cout << "The name of the employee is: " << name << endl;

cout << "The employee number of the employee is: " << empno << endl;

};

class teacher : public employee

public:

string sub;

string pub;

void getdata()

employee::getdata();

cout << "Enter the subject of the teacher: ";

getline(cin >> ws, sub);

cout << "Enter the publication of the teacher: ";

getline(cin >> ws, pub);

void display()

{
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

employee::display();

cout << "The subject of the teacher is: " << sub << endl;

cout << "The publication of the teacher is: " << pub << endl;

};

class typist : public employee

public:

int speed;

void getdata()

employee::getdata();

cout << "Enter the speed of the typist: ";

cin >> speed;

void display()

employee::display();

cout << "The speed of the typist is: " << speed << endl;

};

class officer : public employee

public:

string grade;

void getdata()
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

employee::getdata();

cout << "Enter the grade of the officer: ";

getline(cin >> ws, grade);

void display()

employee::display();

cout << "The grade of the officer is: " << grade << endl;

};

class regular : public typist

public:

void getdata()

typist::getdata();

void display()

typist::display();

};

class casual : public typist

public:
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

int dailywage;

void getdata()

typist::getdata();

cout << "Enter the daily wage of the casual typist: ";

cin >> dailywage;

void display()

typist::display();

cout << "The daily wage of the casual typist is: " << dailywage << endl;

};

int main()

teacher t;

regular r;

casual c;

officer o;

int ch;

cout << "Enter\n(1) For teacher\n(2) For regular typist\n(3) For casual typist\n(4)
For officer: ";

cin >> ch;

cout << endl;

if (ch == 1)

t.getdata();
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

t.display();

else if (ch == 2)

r.getdata();

r.display();

else if (ch == 3)

c.getdata();

c.display();

else if (ch == 4)

o.getdata();

o.display();

else

cout << "Invalid choice" << endl;

cout << endl;

}
SVKM’s NMIMS University
Mukesh Patel School of Technology Management & Engineering
COURSE: Programming for Problem Solving

Conclusion (Learning Outcomes): Reflect on the questions answered by you jot down your
learnings about the Topic: Structure and Union

You might also like