0% found this document useful (0 votes)
7 views

MSINGLA3

Uploaded by

notrealbybit
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)
7 views

MSINGLA3

Uploaded by

notrealbybit
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/ 4

DR. B.

R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY,


JALANDHAR

ASSIGNMENT 3 (OOPC PROGRAMMING EXERCISE)

SUBMITTED BY : MAYANK SINGLA


SUBMITTED TO : MISS.ANUJA

ROLL NUMBER : 23124062


BRANCH : IT
Q1 Create a base class Person with attributes name s.displayStudent();
and age, and a derived class Student that return 0;
inherits from Person and has an additional }
attribute grade. Implement methods to set and
display
these attributes in both classes.

#include <iostream>
using namespace std;

class Person {
protected:
string name; Q2 Create a class BankAccount with attributes
int age; accountNumber and balance. Derive a class
SavingsAccount from BankAccount with an
public: additional method calculateInterest that calculates
void setPerson(string n, int a) { interest based on a fixed interest rate (e.g., 5%) and
name = n; updates the balance. Implement methods to
age = a; deposit and withdraw money in the BankAccount
} class and use them in SavingsAccount.

void displayPerson() { #include <iostream>


cout << "Name: " << name << ", Age: " << age using namespace std;
<< endl;
} class BankAccount {
}; protected:
int accountNumber;
class Student : public Person { double balance;
private:
int grade; public:
void setAccount(int accNum, double bal) {
public: accountNumber = accNum;
void setStudent(string n, int a, int g) { balance = bal;
setPerson(n, a); }
grade = g;
} void deposit(double amount) {
balance += amount;
void displayStudent() { }
displayPerson();
cout << "Grade: " << grade << endl; void withdraw(double amount) {
} if (amount <= balance) balance -= amount;
}; }

int main() { void displayBalance() {


Student s; cout << "Balance: $" << balance << endl;
s.setStudent("John", 20, 90); }
}; void calculateArea() {
double area = 3.14159 * radius * radius;
class SavingsAccount : public BankAccount { cout << "Color: " << color << ", Area: " << area
public: << endl;
void calculateInterest() { }
balance += balance * 0.05; };
}
}; int main() {
Circle c;
int main() { c.setCircle(5.0, "Red");
SavingsAccount sa; c.calculateArea();
sa.setAccount(12345, 1000.0); return 0;
sa.deposit(500); }
sa.calculateInterest();
sa.displayBalance();
return 0;
}

Q4 Create a base class Account with an attribute


balance and methods to deposit and withdraw.
Derive a class CheckingAccount that charges a fee
for each transaction. Implement methods in
CheckingAccount to withdraw with an additional
Q3 Create a base class Shape with a protected transaction fee (e.g., $1). Display the balance
attribute color. Derive a class Circle that has an after each transaction.
additional attribute radius. Implement a method
calculateArea() in Circle to calculate the area of #include <iostream>
the circle and display it along with the color. using namespace std;

class Account {
#include <iostream> protected:
using namespace std; double balance;

class Shape { public:


protected: void setBalance(double bal) {
string color; balance = bal;
}
public:
void setColor(string c) { void deposit(double amount) {
color = c; balance += amount;
} }
};
void withdraw(double amount) {
class Circle : public Shape { if (amount <= balance) balance -= amount;
private: }
double radius;
void displayBalance() {
public: cout << "Balance: $" << balance << endl;
void setCircle(double r, string c) { }
radius = r; };
setColor(c);
} class CheckingAccount : public Account {
public:
void withdrawWithFee(double amount) { }
if (amount + 1 <= balance) balance -= (amount };
+ 1);
} int main() {
}; SavingsAccount sa;
sa.deposit(1000);
int main() { sa.setInterestRate(0.05);
CheckingAccount ca; sa.applyInterest();
ca.setBalance(500); cout << "Balance after interest: $" <<
ca.deposit(200); sa.getBalance() << endl;
ca.withdrawWithFee(100); return 0;
ca.displayBalance(); }
return 0;
}

}Q5 Create a base class BankAccount with a private


attribute balance. Add public methods
deposit() and getBalance() to modify and access
balance. Derive a class SavingsAccount that has
an additional interestRate attribute and a method
applyInterest() to add interest to the balance.

#include <iostream>
using namespace std;

class BankAccount {
private:
double balance;

public:
void deposit(double amount) {
balance += amount;
}

double getBalance() {
return balance;
}
};

class SavingsAccount : public BankAccount {


private:
double interestRate;

public:
void setInterestRate(double rate) {
interestRate = rate;
}

void applyInterest() {
deposit(getBalance() * interestRate);

You might also like