You are on page 1of 13

Chandigarh University

Gharuan, Mohali
Assignment : OOPS
Institute/Department: University Institute of Engineering

Division: Academic Unit-1

Subject Name: Object Oriented Programming Using C++

Subject Code: CST-157

Faculty Name : Sheenam Mam

Submitted By : Prince Kumar UID: 19BCS2768 Branch :CSE25’B

Q1.Define a class to represent a bank account which includes following members :


Data members –

1) Name

2) Account number

3) Type of account

4) Bal. amt

Member functions –

a. To assign initial value

b. To deposit an account

c. To withdraw an account

d. To display name, account number & balance.

Solution : #include<iostream>

#include<stdio.h>
#include<string.h>

using namespace std;

class bank

int acno;

char nm[100], acctype[100];

float bal;

public:

bank(int acc_no, char *name, char *acc_type, float balance)

acno=acc_no;

strcpy(nm, name);

strcpy(acctype, acc_type);

bal=balance;

void deposit();

void withdraw();

void display();

};

void bank::deposit()

int damt1;

cout<<"\n Enter Deposit Amount = ";

cin>>damt1;
bal+=damt1;

void bank::withdraw()

int wamt1;

cout<<"\n Enter Withdraw Amount = ";

cin>>wamt1;

if(wamt1>bal)

cout<<"\n Cannot Withdraw Amount";

bal-=wamt1;

void bank::display()

cout<<"\n ----------------------";

cout<<"\n Accout No. : "<<acno;

cout<<"\n Name : "<<nm;

cout<<"\n Account Type : "<<acctype;

cout<<"\n Balance : "<<bal;

int main()

int acc_no;

char name[100], acc_type[100];

float balance;
cout<<"\n Enter Details: \n";

cout<<"-----------------------";

cout<<"\n Accout No. ";

cin>>acc_no;

cout<<"\n Name : ";

cin>>name;

cout<<"\n Account Type : ";

cin>>acc_type;

cout<<"\n Balance : ";

cin>>balance;

bank b1(acc_no, name, acc_type, balance);

b1.deposit();

b1.withdraw();

b1.display();

return 0;

OUTPUT :-
Q2. What is the difference between friend function and inline function? Explain with
examples.

Solution :

A friend function is used for accessing the non-public members of a class. A


class can allow non-member functions and other classes to access its own
private data, by making them friends. Thus, a friend function is an ordinary
function or a member of another class.
Inline functions are functions where the call is made to inline functions. The
actual code then gets placed in the calling program. The inline function takes
the format as a normal function but when it is compiled it is compiled as inline
code. The function is placed separately as inline function, thus adding
readability to the source program. When the program is compiled, the code
present in function body is replaced in the place of function call.

Q3. Write a function called hms_to_secs() that takes three int values—for hours,
minutes, and seconds—as arguments, and returns the equivalent time in seconds (type
long). Create a program that exercises this function by repeatedly obtaining a time value
in hours, minutes, and seconds from the user (format 12:59:59), calling the function,
and displaying the value of seconds it returns.

Solution: #include<iostream>

using namespace std;

int hms_to_sec(int hr,int min, int sec);

int main()
{

int hr,min,sec;

int result =hms_to_sec(hr,min,sec);

cout <<"\nresult = \n" << result;

system("pause");

return 0;

int hms_to_sec(int hr,int min, int sec)

int seconds =0;

cout << "Please enter Hour" << endl;

cin >> hr;

cout << "Please enter Minutes" << endl;

cin >> min;

cout << "Please enter Seconds" << endl;

cin >> sec;

seconds = (hr*60*60)+(min*60)+sec;

return seconds;

OUTPUT :-
Q4. An electricity board charges the following rates to users – For first 100 units : 40p
per unit – For next 200 units : 50p per unit – Beyond 300 units : 60p per unit All users
are charged a minimum of Rs.150. If the total cost is more than Rs.250.00 then an
additional charges of 15% are added. Write a C++ program using class to read the name
of users & number of units consumed & print out the charges with names.

Solution : #include<iostream>

using namespace std;

class Electric
{

float unit;

char name[20];

public:

void accept()

cout<<"\n Enter Name : ";

cin>>name;

cout<<"\n No. Of Units : ";

cin>>unit;

void print_bill();

};

void Electric::print_bill()

int bill=0;

if(unit>=100 && unit<=300)

bill=(500+(unit*0.40));

else if(unit>100 && unit<=300)

bill=(500+(unit*0.40)+(unit*0.50));

else if(unit>300)

bill=(500+(unit*0.60));

if(bill>250)

bill=(bill+(bill*(15/100)));
cout<<"\n Bill = "<<bill<<"\t"<<name;

int main()

Electric e[10];

int i,cnt;

cout<<"\n Enter How Many Customers You Want? : ";

cin>>cnt;

for(i=0; i<cnt; i++)

e[i].accept();

for(i=0; i<cnt; i++)

e[i].print_bill();

return 0;

OUTPUT :-
Q5. Write a program to find the cube of a number by showing use of inline
function.
Solution :
#include <iostream>

using namespace std;

class cube

int a, acube;

public:

void geta();

inline void cubea();

void show();

};

void cube::geta()

cout << endl << "Enter a number:" ;

cin >> a ;

void cube::cubea()

acube = a*a*a ;
}

void cube::show()

cout << endl << "Entered number(a)=" << a << endl << "Cube of a=" << acube << endl ;

int main()

cube c1;

c1.geta();

c1.cubea();

c1.show();

return 0;

OUTPUT :-

You might also like