Assigment Oop Condense
Assigment Oop Condense
ROLL NO 303-211019
SUBJECT BS(AI)
DATE 04-SEP-2022
REGARDS
GUL AHMAD
Question 1:-
Define a class name “BANK” for a bank account that includes the following data member: Name of the
depositor Account Number Type of Account Balance Amount in the Account
The class also contains the following member functions. A constructor to assign initial values.
Deposit function to deposit some amount. It should accept the amount as parameter. Withdraw
function to withdraw an amount after checking the balance. It should accept the amount as parameter.
Display function to display depositor name and balance.
At the end write a main program and create an object of BANK class and manipulate the account data
using the functions.
-:PROGRAM:-
#include<iostream>
#include<string.h>
class Bank
private:
char Name[30];
char Type_of_Account[30];
float Balance;
public:
Account_No=Accno;
strcpy(Name,name);
strcpy(Type_of_Account,acc_type);
Balance=Bal;
void Deposit()
int D_Amount;
cin>>D_Amount;
Balance+=D_Amount;
void Withdraw()
int W_Amount;
cin>>W_Amount;
Balance-=W_Amount;
void Display()
cout<<"\n-------------------------------";
cout<<"\nAccount Number:"<<Account_No;
cout<<"\nType of Account:"<<Type_of_Account;
cout<<"\nBalance ="<<Balance;
}
};
int main()
int Accno;
char name[30];
char acc_type[30];
float Bal;
cout<<"\nEnter details:";
cout<<"\n-------------------------------";
cin>>Accno;
cout<<"\nName:";
cin>>name;
cout<<"\nAccount Type:";
cin>>acc_type;
cout<<"\nBalance=";
cin>>Bal;
Bank b(Accno,name,acc_type,Bal);
b.Deposit();
b.Withdraw();
b.Display();
return 0;
}
Output:
Question 2:-
Imagine a publishing company that markets both book and audiocassette versions of its works. Create a
class publication that stores the title (a string) and price (type float) of a publication. From this class
derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in
minutes (type float). Each of these three classes should have a getdata() function to get its data from the
user at the keyboard, and a putdata() function to display its data. Write a main() program to test the
book and tape classes by creating instances of them, asking the user to fill in data with getdata(), and
then displaying the data with putdata().
-:PROGRAM:-
#include<iostream>
class Publication
protected:
char Title[30];
float Price;
public:
void getdata()
cout<<"Title:";
cin>>Title;
cout<<"\nPrice=";
cin>>Price;
void putdata()
cout<<"\nTitle:"<<Title;
cout<<"\nPrice="<<Price;
};
private:
int Page_Count;
public:
void getdata()
Publication::getdata();
cout<<"\nTotal Pages=";
cin>>Page_Count;
}
void putdata()
Publication::putdata();
};
private:
float Play_Time;
public:
void getdata()
Publication::getdata();
cin>>Play_Time;
void putdata()
Publication::putdata();
};
int main()
Book B;
Audio_Cassete C;
B.getdata();
C.getdata();
B.putdata();
C.putdata();
return 0;
Output:
Question 3:-
Read the following program code carefully and answer the questions given at the end of program
code.
1. #include <iostream> 2. using namespace std; 3. class Shape // Declaration of base class. 4.
{ protected: 5. int a,b; 6. public: 7. void input(int n,int m) 8. { a= n; 9. b = m; }
10. }; 11. class Rectangle : public Shape // inheriting Shape class 12. { 13. public: 14. int
rectangle_area() 15. { int area = a*b; 16. return area; } 17. }; 18. class Triangle : public Shape
// inheriting Shape class 19. { public: 20. int triangle_area() 21. { float area =
0.5*a*b; 22. return area; } 23. }; 24. int main() 25. { Rectangle r; 26. Triangle t; 27. int
length,breadth,base,height; 28. cout << "Enter the length and breadth of a rectangle: " << endl; 29.
cin>>length>>breadth; 30. r.input(length,breadth); 31. cout << "Area of the rectangle is : " <<
r.rectangle_area()<<endl; 32. cout << "Enter the base and height of the triangle: " << endl; 33.
cin>>base>>height; 34. t.input(base,height); 35. cout <<"Area of the triangle is : " <<
t.triangle_area()<<endl; 36. return 0; }
ANSWER:
i):-
In the above program the name of Base Class is ‘Shape’.
ii):-
This program contains three classes. One is base while the other twos are derived classes from
the one.The names of derived classes are ‘Triangle and Rectangle’.
iii):-
In the given above program the base class declare integers data members in its protected block,
(derived classes can access).i.e a and b are length and width of any shape, and member functions in its
public block. A constructor initializes ‘a’ and ‘b’ with ‘n’ and ‘m’.
iv):-
The first derived class “Rectangle” inheriting the base class “Shape” and get an access to the
data members of its base class. In derive class ‘Rectangle’ a member function is created publically to get
the area of rectangle in integer form and return this area to object in the main function.
v):-
In the second derived class “Triangle” inheriting the base Shape class to get access to its data
members and can perform execution on them.In Class Triangle member function getting the area of
Triangle by giving a direct value of its side in float form and the others two are taken from the base class.
It return float value to its object in main program.
vi):-
In Main function, first of all we create two objects for both derived class i.e. for Class Rectangle
we create ‘r’ and for Class Triangle we create ‘t’ as an object. So it allocate the memory of class to its
object. After that we declare four variables (length, breadth, height, base) for the shapes on which we
are working. And input the length and breadth from the user to get the area of rectangle. These inputs
from the user first pass to the function of Base Class Shapes and then pass to derived class Rectangle to
calculate the area and give it back to the main function and to display on the screen. After this it again
asked for the values for Class Triangle from the user. The values for Base and Height will take from the
user and handover to the base class and then for execution of its area pass to the derived class Triangle.
The third side value is given directly in its function. After execution the the area of Triangle will display
on the screen. At the end the ‘return (0)’ statement gives control back to operating system that the
program terminates normally.
COMPLETED