Download Bahria university OOP Assignment 4 and more Assignments Computer Science in PDF only on Docsity!
Task 1:
Define a Class BOOK with the following specifications: Data Members:bookNo bookTitle
price.
#include using namespace std; class Book { public: int bookNo; string bookTitle; float price; void takedata(){ cout<<"\n Enter the book no : "; cin>>bookNo; cout<<"\n Enter the book title : "; cin>>bookTitle; cout<<"\n Enter the book price : "; cin>>price; } void displaydata() { cout<<"\n Book No : "<<bookNo; cout<<"\n Book Title : "<<bookTitle; cout<<"\n Book Price : "<<price; } void purchaseinfo(Book B[5]) { int bookNo, copies, price = 0, flag = 0; float amount = 0.0; cout<<"\n Please enter the Book No you wish to buy : "; cin>>bookNo; cout<<"\n Please enter the number of copies : "; cin>>copies; for(int i=0;i<5;i++)
{ if(B[i].bookNo == bookNo) { price = B[i].price; flag = 1; } } if(flag == 0) { cout<<"\n Please enter a valid book no : "; } else {amount = price*copies; cout<<"\n The total amount of your purchase is : "<<amount; } }}; int main() { Book B[5]; int option = 1; for(int i=0;i<5;i++) { B[i].takedata(); } do { cout<<"\n Please choose from the below options."; cout<<"\n 1. Display all the books info"; cout<<"\n 2. Make a purchase"; cout<<"\n 0. Exit"; cout<<"\n Please enter you choice : "; cin>>option; if(option == 1) { for(int i=0;i<5;i++) { cout<<"\n Data for book "<<i+1; B[i].displaydata(); } } else if(option == 2)
Task 2:
Implement the given class diagram.
-a:int
-b:int
+setdata(int,int):voi
d
+getdata(Test
t[ ]):void
#include using namespace std; class Test { private: int x; int y; public: void setData(int x, int y) { x = x; y = y; cout << "The Values of x and y are: " << x << " " << y;} void getData(Test t[]) { t[0].setData(20, 43);}}; int main() { Test t[1]; t[0].getData(t); return 0;}
OUTPUT:
Task 3:
Write a program to add, subtract and multiply two complex numbers using class object to
function.
#include using namespace std; class Complex{ private: double real,imag; public: Complex(){ real=imag=0; } Complex(double r){ real=r; imag=0; } Complex(double r, double i){ real=r; imag=i; } Complex(Complex &obj){ real=obj.real; imag=obj.imag; } Complex add(Complex c){ Complex Add; Add.real = real + c.real; Add.imag = imag + c.imag; return Add;}
cout<<"Enter the imaginary part of First Number: "; cin>>imag1; Complex obj1(real1,imag1); obj1.print(); cout<<"Enter the Real part of Second Number: "; cin>>real2; cout<<"Enter the Imaginary part of second number: "; cin>>imag2; Complex obj2(real2,imag2); obj2.print(); Complex c; c = obj1.add(obj2); cout<<"Addition is : ("<<c.getReal()<<")+("<<c.getImag()<<")i"<<endl; c= obj1.sub(obj2); cout<<endl<<"Subtraction is : ("<<c.getReal()<<")+("<<c.getImag()<<")i"<<endl; c= obj1.mult(obj2); cout<<endl<<"Multiplication is : ("<<c.getReal()<<")+("<<c.getImag()<<")i"<<endl; return 0; }
OUTPUT:
Task 4:
Write a program with a class that contains following data members Id name marks Create
array of objects of size 10. Create a function read () to take input from user. Create a
function to calculate percentage of each student and a function that displays the id and
name of the student that have the lowest percentage.
#include using namespace std; class student { public: int id; string name; int marks; int tMarks; int finalMarks; void read() { cout << "Student ID: "; cin >> id; cout << "Name: "; cin >> name; cout << "Marks: "; cin >> marks; } void obtained() { tMarks = 100; finalMarks = (marks * 100) / tMarks; } void print(student obj[10]) { int n = obj[0].finalMarks; int i = 0; int j = 0;