OOP Lab

Created by
BBorhan
Last edited time
Tag
#include<bits/stdc++.h>
using namespace std;

double power(double m, int n=2){
	return pow(m, n);
}

int main(){
	int t;	cin >> t;
	while(t--){	
		double m; int n;	cin >> m >> n;	
		cout << power(m, n) << "\n";
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

inline int large(int a, int b, int c){
	return max({a, b, c});
}

int main(){
	int a, b, c;	cin >> a >> b >> c;
	cout << large(a, b, c) << "\n";
	
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

const int MAX = 1e5+5;

class Order{
	pair<int, double> arr[MAX];
	int n;
	
	public:
	Order(){
		n=0;
	}
	
	void addOrder(int code, double price){
		arr[n] = make_pair(code, price);
		n++;
	}
	
	void deleteOrder(int code){
		int ind=-1;
		for(int i=0; i<n; i++){
			if(arr[i].first == code){
				ind = i;	break;
			}
		}
		
		if(ind == -1){
			cout << "Code not found\n";
			return;
		}
		
		for(int i=ind; i<n; i++){
			arr[i]=arr[i+1];
		}
		n--;
	}
	
	void display(){
		cout << n << "\n";
		for(int i=0; i<n; i++){
			int code  = arr[i].first;	
			int price = arr[i].second;
			cout << code << " " << price << "\n";
		}
	}
		
};


int main(){
	cout << "First Input the test Cases\n1 - Adding Items\n2 - Deleting Item \n3 - Display\n";
	int t;	cin >> t;
	Order order;
	while(t--){
		int operation;	cin >> operation;
		int code; 	double price;
		
		if(operation == 1){
			cin >> code >> price;
			order.addOrder(code, price);
		} else if(operation == 2){
			cin >> code;
			order.deleteOrder(code);
		} else{
			order.display();
		}
	}
}

#include<bits/stdc++.h>
using namespace std;

class Student{
	string name; int roll; double grade;
	
	public:
	void get(string name, int roll, double grade){
		this->name = name;
		this->roll = roll;
		this->grade = grade;
	}
	
	void display(){
		cout << name << " " << roll << " " << grade << "\n";
	}
};

int main(){
	Student students[3];
	string name; int roll; double grade;
	
	for(int i=0; i<3; i++){
		cin >> name >> roll >> grade;
		students[i].get(name, roll, grade);
	}
	
	for(int i=0; i<3; i++){
		students[i].display();
	}
}

#include<bits/stdc++.h>
using namespace std;

class Time{
	int hour, minute;
	
	public:
	Time(){
		hour=minute=0;
	}
	
	void get(){
		cin >> hour >> minute;
	}
	
	void sum(Time t1, Time t2){
		int totalHour = t1.hour + t2.hour;
		int totalMinute = t1.minute + t2.minute;
		this->hour = totalHour + (totalMinute/60);
		this->minute = totalMinute%60;
	}
	
	void display(){
		cout << hour << " hours " << minute << " minutes\n";
	}
};

int main(){
	Time t1, t2, t3;
	
	t1.get();
	t2.get();
	t3.sum(t1, t2);
	
	t1.display();
	t2.display();
	t3.display();
	
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

class BankAccount{
	string name, type, account_number;
	int balance;
	
	public:
	
	void get(string n, string t, string num, int balance){
		name = n;
		type = t;
		account_number = num;
		this->balance = balance;
	}
	
	void deposit(int amount){
		balance  += amount;
		cout << "The deposit of " << amount << " taka has been executed successfully.\n";
	}
	
	void withdraw(int amount){
		if(amount > balance){
			cout << "You have not sufficient balacne to withdraw " << amount << " \n";
			return;
		}
		balance -= amount;
		cout << "A withdrawal of "  << amount << " taka was completed successfully.\n";
	}
	
	void display(){
		cout << "Name : " << name << "\n";
		cout << "Balance : " << balance << "\n";
	}
};


int main(){
	string name, acc_number, type; int balance;
	cin >> name >> type >> acc_number >> balance;
	
	BankAccount b;
	b.get(name, type, acc_number, balance);
	
	b.display();
	
	b.deposit(200);
	b.display();
	
	b.withdraw(299);
	b.display();
	
	b.withdraw(100);
	
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

const int MAX = 1e4+5;

class BankAccount{
	string name[MAX], type[MAX], account_number[MAX];
	int balance[MAX];
	int n;
	public:
	BankAccount(){
		n=0;
	}
	
	void get(string nm, string t, string num, int balance){
		name[n] = nm;
		type[n] = t;
		account_number[n] = num;
		this->balance[n] = balance;
		n++;
	}
	
	int check(string acc_num){
		int ind = -1;
		for(int i=0; i<n; i++){
			if(account_number[i]==acc_num){
				ind=i; break;
			}
		}
		
		if(ind == -1) {
			cout << "Acount is not found.\n";
		}		
		return ind;
	}
	
	void deposit(string acc_num, int amount){
		int ind = check(acc_num);
		if(ind == -1) return;
		
		balance[ind]  += amount;
		cout << "The deposit of " << amount << " taka in " << acc_num << " has been executed successfully.\n";
	}	

	void withdraw(string acc_num, int amount){
		int ind = check(acc_num);
		if(ind == -1) return;
				
		if(amount > balance[ind]){
			cout << "You have not sufficient balacne to withdraw " << amount << " \n";
			return;
		}
		balance[ind] -= amount;
		cout << "A withdrawal of "  << amount << " taka was completed successfully.\n";
	}	
	
	
	void display(string acc_num){
		int ind = check(acc_num);
		if(ind == -1) return;
		
		cout << "Name : " << name[ind] << "\n";
		cout << "Balance : " << balance[ind] << "\n";
	}
	

};


int main(){
	string name, acc_number, type; int balance;
	
	BankAccount b;
	for(int i=0; i<10; i++){
		cin >> name >> type >> acc_number >> balance;
		b.get(name, type, acc_number, balance);
	}
	
	int q;	cin >> q;
	cout << "1. Display \n2. Deposit\n3. Withdraw\n";
	
	while(q--){
		int op;	cin >> op;
		
		if(op==1){
			cin >> acc_number;
			b.display(acc_number);
		} else if(op==2){
			cin >> acc_number >> balance;
			b.deposit(acc_number, balance);
		} else if(op == 3){
			cin >> acc_number >> balance;
			b.withdraw(acc_number, balance);			
		}
	}
	
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

class Vector{
	int size;
	float *p;
	
	public:
	
	Vector(){
		cin >> size;
		p = new float [size];
		
		for(int i=0; i<size; i++)
			cin >> p[i];
	}
	
	void modify(int n, int value){
		p[n-1]=value;
	}

	void multiply(int n){
		for(int i=0; i<size; i++){
			p[i] *= n;
		}
	}
	
	void display(){
		for(int i=0; i<size; i++){
			cout << p[i] << " ";
		}
		cout << "\n";
	}
	
	~Vector(){
		delete []p;
	}
};


int main(){
	Vector v;
	
	v.display();
	
	v.multiply(10);
	v.display();
	
	v.modify(2, 6);
	v.display();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

class Vector{
	int size;
	float *p;
	
	public:
	Vector(){
		size = 1;
		p = new float[1];
		p[0]=1;
	}
	Vector(int size){
		this->size = size;
		p = new float [size];
		
		for(int i=0; i<size; i++)
			cin >> p[i];
	}
	
	void modify(int n, int value){
		p[n-1]=value;
	}

	void multiply(int n){
		for(int i=0; i<size; i++){
			p[i] *= n;
		}
	}
	
	void display(){
		for(int i=0; i<size; i++){
			cout << p[i] << " ";
		}
		cout << "\n";
	}
	
	void add(Vector &v1, Vector &v2){
		
		int sz1 = v1.size;
		int sz2 = v2.size;
		
		size = max(sz1, sz2);
		
		int mn = min(sz1, sz2);
		p = new float[max(sz1, sz2)];
		
		for(int i=0; i < mn; i++){
			p[i] = v1.p[i]+v2.p[i];
		}
		for(int i = min(sz1, sz2); i<size; i++){
			if(sz1 > sz2){
				p[i] = v1.p[i];
			} else p[i]=v2.p[i];
		}
	}
	
	~Vector(){
		delete []p;
	}
};


int main(){
	Vector v1(3), v2(2);
	v2.display();
	
	Vector v3;
	v3.add(v1, v2);
	v3.display();
	
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

class DB;

class DM {
	double m, cm;
	
	public:
	DM(){
		m=0; cm=0;
	}
	void get(double x, double y){
		m=x; cm=y;
	}
	double fttom(double x){
		return x*0.3048;
	}
	double inchtocm(double x){
		return x*2.54;
	}
	void display(){
		cout << m << " m " << cm << " cm\n";
	}	
	friend void add_DM(DM &dm, DB &db);
	friend void add_DB(DM &dm, DB &db);
};

class DB {
	double ft, inch;
	
	public:
	DB(){
		ft=0; inch=0;
	}
	void get(double x, double y){
		ft=x; inch=y;
	}
	double mtoft(double x){
		return x*3.28;
	}
	double cmtoinch(double x){
		return x/2.54;
	}
	void display(){
		cout << ft << " feet " << inch << " inch\n";
	}
	friend void add_DB(DM &dm, DB &db);
	friend void add_DM(DM &dm, DB &db);
};

	
void add_DM(DM &dm, DB &db){
	DM d1;
	d1.m = dm.m + d1.fttom(db.ft);
	d1.cm = dm.cm + d1.inchtocm(db.inch);
	
	d1.display();
}

void add_DB(DM &dm, DB &db){
	DB d2;
	d2.ft = db.ft + d2.mtoft(dm.m);
	d2.inch = db.inch + d2.cmtoinch(dm.cm);
		
	d2.display();	
}


int main(){
	DM dm; DB db;
	
	double x, y;
	cout << "Input m and cm: \n";
	cin >> x >> y; dm.get(x, y);
	cout << "Input feet and inch: \n";
	cin >> x >> y; db.get(x, y);	
	
	add_DM(dm, db);	add_DB(dm, db);
}

#include<bits/stdc++.h>
using namespace std;

class Bank{
	double principle, interest_rate, return_value, year;
	
	public:
	Bank(){
		cout << "Input Principle amount, interest rate and year of investment\n";
		cin >> principle >> interest_rate >> year;
		return_value = principle*pow(1+(interest_rate/100), year);
	}
	
	void display(){
		cout << "Principle Amount : " << principle << "\n";
		cout << "Return value: " << return_value << "\n";
	}
};

int main(){
	Bank bn;
	bn.display();
		
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

class Books{
	char *title, *author, *publisher;
	float price;
	int stock;
	
	public:
	Books();
	void get();
	bool search(string t, string a);
	void buy();
	void display(){
		cout << title << " " << author << "\n";
	}
};

Books::Books(){
	title = new char[150];
	author = new char[150];
	publisher = new char[150];	
}
void Books::get(){
	cin >> title >> author >> publisher ;
	cin >> price >> stock;
}
bool Books::search(string t, string a){
	if(title == t && a==author){
		cout << "Title : " << title << "\n";
		cout << "Author : " << author << "\n";
		cout << "Publisher : " << publisher << "\n";
		cout << "Price : " << price << "\n";
		cout << "Stock : " << stock << "\n";
		return true;
		
	} else{
		return false;
	}
}
void Books::buy(){
	cout << "Input for required copies << " << "\n";
	int x;	cin >> x;
	
	if(x <= stock){
		cout << x << " copies are available.\n";
		cout << "Total Cost : " << x*price << "\n";
	} else{
		cout << "Required copies not in stock\n";
		return;
	}
}


int main(){
	int n;
	cout << "Total Number of books : \n";
	cin >> n;
	Books books[n];
	for(int i=0; i<n; i++){
		books[i].get();
	}
	
	char *title = new char[150];
	char *author = new char[150];
	
	cin >> title >> author;
	bool found = false;
	for(int i=0; i<n; i++){
		if(books[i].search(title, author)){
			books[i].buy();
			found = true;
			break;
		}
	}
	
	if(!found){
		cout << "Books not found.";
	}
	
}

#include<bits/stdc++.h>
using namespace std;

class Complex{
	double i,j;
	
	public:
	Complex(){
		i=0; j=0;
	}
	
	void get(){
		cout << "Input real and imaginary part:\n";
		cin >> i >> j;
	}
	
	Complex operator+(const Complex &a){
		Complex x;
		x.i = i + a.i;
		x.j = j+a.j;
		return x;
	}
	
	void operator=(const Complex &b){
		i=b.i;
		j=b.j;
	}
	
	Complex operator-(const Complex &a){
		Complex x;
		x.i = i - a.i;
		x.j = j - a.j;
		return x;
	}
	
	void display(){
		cout << i << (j >= 0 ? "+" : "-") << abs(j) << "i" << "\n";
	}
};

int main(){
	Complex a, b;
	a.get(); b.get();
	
	Complex c;
	c = a+b;
	c.display();
	
	c = a-b;
	c.display();
	
	c=a;
	c.display();
}

#include<bits/stdc++.h>
using namespace std;

class Complex{
	double i,j;
	
	public:
	Complex(){
		i=0; j=0;
	}
	
	void get(){
		cout << "Input real and imaginary part:\n";
		cin >> i >> j;
	}
	
	Complex operator+(const Complex &a){
		Complex x;
		x.i = i + a.i;
		x.j = j+a.j;
		return x;
	}
	
	void operator=(const Complex &b){
		i=b.i;
		j=b.j;
	}
	
	Complex operator-(const Complex &a){
		Complex x;
		x.i = i - a.i;
		x.j = j - a.j;
		return x;
	}
	
	Complex operator*(const Complex &a){
		Complex x;
		x.i = (i*a.i - j*a.j);
		x.j = (i*a.j + j*a.i);
		
		return x;
	}
	
	Complex operator/(const Complex &y){
		Complex x;
		double a=y.i; double b=y.j;
		double c=i;	double d=j;
		x.i = (c*a + b*d)/(a*a + b*b);
		x.j = (a*d - c*b)/(a*a - c*b);
		
		return x;
	}
	
	
	void display(){
		cout << i << (j >= 0 ? "+" : "-") << abs(j) << "i" << "\n";
	}
};

int main(){
	Complex a, b;
	a.get(); b.get();
	
	Complex c;
	c = a+b;
	c.display();
	
	c = a*b;
	c.display();
	
	c = a/b;
	c.display();
	
	c=a;
	c.display();
}

#include<bits/stdc++.h>
using namespace std;

class student{
	protected:
		int roll;
	
	public:
	student(){
		cout << "Input Roll : \n";
		cin >> roll;
	}
};

class test{
	protected:
	int num1, num2;
	
	public:
	test(){
		cout << "Input subject 1 and 2 marks respectively\n";
		cin >> num1 >> num2;
	};	
};

class result : public student, public test{
	int totalMarks;
	
	public:
	result() {
		totalMarks = num1+num2;
	}
	void display(){
		cout << "\nRoll : " << roll << "\n";
		cout << "Marks in two subject : " << num1 << ", " << num2 << "\n";
		cout << "Total Marks : " << totalMarks << "\n";
	}
};

int main(){
	
	result res;
	res.display();
}

#include<bits/stdc++.h>
using namespace std;

class account{
	protected:
	string name, acc_num, acc_type;
	int balance;
	
	public:
	account(string name, string acc_num, string acc_type, int balance){
		this->name = name;
		this->acc_num = acc_num;
		this->acc_type = acc_type;
		this->balance = balance;
	}
	
	void deposit(int amount){
		balance += amount;
		cout << "A deposit to " << acc_num << " executed successfully\n";
	}
	
	void display(){
		cout << "Balance : " << balance << "\n";
	}
	
	
};

class cur_acct : public account{
	int interest_rate, min_balance, penalty;
	public:
	cur_acct(string name, string acc_num, int balance, int rate, int min_balance, int penalty) : account(name, acc_num, "Current", balance){
		interest_rate=rate/100;
		this->min_balance = min_balance;
		this->penalty = penalty;
	}
	
	void checkInterest(){
		int interest = balance*(1+interest_rate) - balance;
		balance += interest;
		cout << "New balance : " << balance << "\n";
	}
	
	void checkPenalty(){
		if(balance < min_balance){
			balance -= penalty;
			cout << "Penalty for having less than minimum balance.\n";
		} else{
			cout << "No penalty\n";
		}
	}
	
	void withdraw(int amount){
		if(amount > balance){
			cout << "Your balance is insufficient.\n";
		} else{
			balance -= amount;
			cout << "Withdraw successfull\n";
			checkPenalty();
		}
	}	
	
};

class sav_acct : public account{
	
	public:
	sav_acct(string name, string acc_num, int balance) : account(name, acc_num, "Savings", balance){
		
	}	
	
	void withdraw(int amount){
		if(amount > balance){
			cout << "Balance is insufficient to withdraw\n";
		} else{
			balance -= amount;
			cout << "Withdraw Successfully\n";
		}
	}
};

int main(){
	cout << "Saving account \n";
	sav_acct acc1("Borhan", "008",  1000);
	acc1.display();
	acc1.deposit(200);
	acc1.display();
	acc1.withdraw(500);
	acc1.display();
	acc1.withdraw(701);
	
	cout << "\n\nCurrent account \n";
	cur_acct acc2("Borhan", "0100", 500, 40, 300, 20);
	acc2.display();
	acc2.deposit(400);
	acc2.withdraw(200);
	acc2.display();
	acc2.withdraw(405);
	acc2.display();
	
}