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

OOP Lab Programs

Lab

Uploaded by

abhishekcd416
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

OOP Lab Programs

Lab

Uploaded by

abhishekcd416
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Create a class 'Bank' which includes data members Acc_no, Name, Balance and
Parameterized Constructor to initialize the data members and other methods like deposit,
withdrawal, and display the detail of the customer. (Note: Minimum balance of Rs. 500/-
should be maintained.)

#include <iostream>
#include <string>
using namespace std;

class Bank {
private:
string name; double amount; int accNo; static const double minBal;

public:
Bank(string n, double amt, int accNo) : name(n), amount(amt), accNum(accNo) {}
void displayDetails() {
cout << "Name: " << name << "\n Account Number: " << accNo << endl;
cout << "Balance: " << amount << " Rs" << endl;
}
void withdraw(double amt) {
if (amount - amt < minBal) {
cout << "Insufficient balance! Minimum balance of " << minBal << " Rs must be
maintained." << endl;
} else {
amount -= amt; cout << "Amount: Rs. " << amt << " withdrawn successfully." << endl;
}
}
void deposit(double amt) {
amount += amt; cout << "Amount: Rs. " << amt << " deposited successfully." << endl;
}
};

const double Bank::minBal = 500.0;

int main() {
Bank account ("John Doe", 1000.0, 123456);
cout << "Initial Details:" << endl;
account.displayDetails(); cout << endl;
account.withdraw (200.0);
cout << "After Withdrawal:" << endl;
account.displayDetails(); cout << endl;
account.deposit (700.0);
cout << "After Deposit:" << endl;
account.displayDetails(); return 0;
}
2. Write a C++ program to accept two real & imaginary parts of Complex numbers to
perform the Multiplication and Division using member functions. Display the result.

#include <iostream>
using namespace std;

class Complex {
private: double real, imag;

public: Complex(double real, double imag) : real(real), imag(imag) {}

void display() { cout << real << " + " <<"("<< imag<<")" << "i"; }

Complex multiply (Complex other) {


double real_result = real * other.real - imag * other.imag;
double imag_result = real * other.imag + imag * other.real;
return Complex(real_result, imag_result);
}

Complex divide (Complex other) {


double denominator = other.real * other.real + other.imag * other.imag;
double real_result = (real * other.real + imag * other.imag) / denominator;
double imag_result = (imag * other.real - real * other.imag) / denominator;
return Complex(real_result, imag_result);
}
};

int main() {
Complex num1(3, 2); Complex num2(1, -1);

Complex result_mul = num1.multiply(num2);


cout << "Multiplication result: "; result_mul.display(); cout << std::endl;

Complex result_div = num1.divide(num2);


cout << "Division result: "; result_div.display(); cout << std::endl;
return 0;
}
3. Write a C++ program to create a class with data members like Principle amount, Time
and Rate of Interest. Create a member function to accept data values, to compute
Compound Interest and to display the result.

#include <iostream>
#include<iomanip>
#include <cmath>
using namespace std;
class CompCalc{
private:
double principal, rate; int time, compFreq;

public:
CompCalc (double p, double r, int t, int n): principal(p), rate(r/100), time(t), compFreq(n) {}

double calculateCI() {
double amount = principal * pow(1 + rate / compFreq, compFreq * time);
return amount;
}
};

int main() {
double principal, rate; int time, compFreq;
cout << "Enter the principal amount: "; cin >> principal;
cout << "Enter the annual interest rate (in decimal): "; cin >> rate;
cout << "Enter the time in years: "; cin >> time;
cout << "Enter the compound frequency per year: "; cin >> compFreq;

CompCalc calculator(principal, rate, time, compFreq);


double amount = calculator.calculateCI();

cout << fixed << setprecision(2);


cout << "The amount after " << time << " years will be: " << amount << endl;
return 0;
}
4. Write a C++ program to create a class with data members a, b, c and member functions
to input data, Compute Discriminants based on the following conditions & print the
roots. If discriminant = 0, print the roots are equal and their value. -- If discriminant > 0,
print the real roots and their values. -- If discriminant < 0, print the roots are imaginary
and exit the program.

#include<iostream>
#include <cmath>
using namespace std;

class QuadraticEquation {
private: double a, b, c;

public:
QuadraticEquation(double a, double b, double c) : a(a), b(b), c(c) {}
double calcDisc() { return b * b – 4 * a * c; }
void checkRoots() {
double disc = calcDisc();

if (disc > 0) {
double r1 = (–b + sqrt(disc)) / (2 * a);
double r2 = (–b – sqrt(disc)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << " Root 1 = " << r1 << " Root 2 = " << r2 << endl;
} else if (disc == 0) {
double root = –b / (2 * a);
cout << "Roots are real and equal." << endl;
cout << "Root = " << root << endl;
} else {
double realPart = –b / (2 * a);
double imagPart = sqrt(-disc) / (2 * a);
cout << "Roots are imaginary." << std::endl;
cout << "Root 1 = " << realPart << " + " << imagPart << "i" << endl;
cout << "Root 2 = " << realPart << " – " << imagPart << "i" << endl;
}
}
};

int main() {
double a, b, c;
cout << "Enter coefficients a, b, and c: "; cin >> a >> b >> c;
QuadraticEquation equation(a, b, c);
equation.checkRoots(); return 0;
}
5. Write a C++ program to compute the Total marks and declare the results using Array of
Objects. Assume that the class contains the data members - Roll no, Name, Marks in 3
subjects. Result is calculated as follows. If student gets < 40 fail. Otherwise various results
are calculated on Average with following conditions. >= 70 Distinction. >= 60 and 70 First
Class. >= 50 and 60 Second Class else Pass.

#include <iostream>
#include <string>
using namespace std;

class Student {
private: string name; int rollNo, marks[3]; float average;
public: Student(std::string n, int r, int m1, int m2, int m3) : name(n), rollNo(r) {
marks[0] = m1; marks[1] = m2; marks[2] = m3; average = (m1 + m2 + m3) / 3.0;
}

bool isPassed() {
for (int i = 0; i < 3; ++i) {
if (marks[i] < 40) { return false; }
} return true;
}

char assignGrade() {
if (average >= 90) { return 'A'; }
else if (average >= 80) { return 'B'; }
else if (average >= 70) { return 'C'; }
else if (average >= 60) { return 'D'; }
else { return 'F'; }
}

void displayResult() {
cout << "Name: " << name << "Roll No: " << rollNo << endl;
cout << "Marks: " << marks[0] << ", " << marks[1] << ", " << marks[2] << endl;
cout << "Average: " << average << endl;
if (isPassed())
{ cout << "Result: Passed, Grade: " << assignGrade() << endl; }
else { cout << "Result: Failed" << endl; } cout << endl;
}
};

int main() {
Student students[3]={ Student("Gaja", 101, 85, 90, 88), Student("Hari", 102, 70, 65, 75),
Student("Mahesh", 103, 45, 50, 35) };
for (int i = 0; i < 3; ++i) { students[i].displayResult(); } return 0;
}

You might also like