0% found this document useful (0 votes)
118 views25 pages

Program To Show Addition of Two Objects

The document contains code for a C++ program that defines two classes - a bank class with data members for customer and account details, and methods to read, withdraw, deposit and display details. It demonstrates creating a bank object, reading input, allowing withdrawal or deposit, and

Uploaded by

Ankit Chatterjee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views25 pages

Program To Show Addition of Two Objects

The document contains code for a C++ program that defines two classes - a bank class with data members for customer and account details, and methods to read, withdraw, deposit and display details. It demonstrates creating a bank object, reading input, allowing withdrawal or deposit, and

Uploaded by

Ankit Chatterjee
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Program to show addition of two objects

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class A
{ int a,b;
public:
void getdata(int a1,int b1)
{
a=a1;
b=b1;
}
void add(A ob1,A ob2)
{ a=ob1.a+ob2.a;
b=ob1.b+ob2.b;
cout<<"\nvalue of a after addition:"<<a;
cout<<"\nvalue of b after addition:"<<b;
}
};
void main()
{ clrscr();
A ob1,ob2,ob3;
ob1.getdata(7,8);
ob2.getdata(9,10);
ob3.add(ob1,ob2);
getch();
}

output:
value of a after addition:16
value of b after addition:18
//wap to construct a class named bank with details-name,type of account,accnt no.
,balance amt.member functions are-read,withdraw,deposite,display//

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class bank
{
char d_name[30];
char a_name[20];
int acc_no;
int a_bal,d_bal,w_bal;

public:
void read()
{
cout<<"\nEnter the name of depositor :";
gets(d_name);
cout<<"\nEnter the type of account :";
gets(a_name);
cout<<"\nEnter the account number :";
cin>>acc_no;
cout<<"\nEnter the balance amount : ";
cin>>a_bal;
}
void withdraw()
{
cout<<"\nEnter the amount to withdraw : ";
cin>>w_bal;
if (a_bal <= 2000)
cout<<"\nYou cannot withdraw as you reached minimum balance ";
else
a_bal = a_bal - w_bal;
}
void deposit()
{
cout<<"\n Enter the amount to deposit : ";
cin>>d_bal;
a_bal = a_bal + d_bal;
}
void display()
{
cout<<"\nThe depositor name is : "<<d_name;
cout<<"\nThe account type is : "<<a_name;
cout<<"\nThe account number is : "<<acc_no;
cout<<"\nThe account balance is : "<<a_bal;
}
};
void main()
{
bank b;
int select;
clrscr();
b.read();
cout<<"\nPress 1. for deposit or Press 2. for withdrawal : ";
cin>>select;
if (select == 1)
b.deposit();
else
if (select == 2)
b.withdraw();
else
cout<<"\nWrong value entered";
b.display();
getch();
}

output:

Enter the name of depositor :ram

Enter the type of account :saving

Enter the account number :456

Enter the balance amount : 6000

Press 1. for deposit or Press 2. for withdrawal : 2

Enter the amount to withdraw : 3000

The depositor name is : ram


The account type is : saving
The account number is : 456
The account balance is : 3000
Wap to create a class book that has the following details:
-name of the book
-author's name
-book price
-no. of pages
member functions are:read(),display().
also create array of 3 books & display their details//

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class book
{
char b_title[20];
char a_name[20];
int price;
int pages;
public:
void display();
void read()
{
cout<<"\nEnter name of book : " ;
gets(b_title);
cout<<"\nEnter name of Author : ";
gets(a_name);
cout<<"\nEnter price of the book : ";
cin>>price;
cout<<"\nEnter the number of pages in the book : ";
cin>>pages;
cout<<"\n**********************************";
}
};
void book :: display()
{
cout<<"\n\n The name of the book is : "<<b_title;
cout<<"\n\n The name of the author is : "<<a_name;
cout<<"\n\n The book price is : "<<price;
cout<<"\n\n The number of pages in the book is : "<<pages;
cout<<"\n\n----------------------------------------------";
}
void main()
{
book b1[3];
clrscr();
for (int i=0;i<3;i++)
{
b1[i].read();
}
for (int j=0;j<3;j++)
{
b1[j].display();
}
getch();
}

output:

Enter name of book : financial accounting

Enter name of Author : t.s. gerewal

Enter price of the book : 350

Enter the number of pages in the book : 350

**********************************
Enter name of book : computer system architecture

Enter name of Author : morris mano

Enter price of the book : 300

Enter the number of pages in the book : 350

**********************************
Enter name of book : c++ fundamentals

Enter name of Author : sumita arora

Enter price of the book : 200

Enter the number of pages in the book : 250

**********************************

The name of the book is : financial accountingt.s. gerewal

The name of the author is : t.s. gerewal


The book price is : 350

The number of pages in the book is : 350

----------------------------------------------

The name of the book is : computer system archmorris mano

The name of the author is : morris mano

The book price is : 300

The number of pages in the book is : 350

----------------------------------------------

The name of the book is : c++ fundamentals

The name of the author is : sumita arora

The book price is : 200

The number of pages in the book is : 250


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class book
{
protected:
int exno,cost;
char name[10],title[20];
public:
void getdata()
{
cout<<"Enter the excession no.:";
cin>>exno;
cout<<"Enter the name of the book:";
gets(title);
cout<<"Enter the author's name:";
gets(name);
cout<<"Enter the cost of the book:";
cin>>cost;
}
void put()
{
cout<<"The excession no. of the book is:"<<exno;
cout<<"\nThe name of the book is:"<<title;
cout<<"\nThe name of the author is:"<<name;
cout<<"\nThe cost of the book is:"<<cost;
}
};
class moreinfo:public book
{
int yr;
char pname[10];
public:
void getdetails()
{
getdata();
cout<<"Enter the name of the publisher:";
gets(pname);
cout<<"\nEnter the year of publication:";
cin>>yr;
}
void showdetails()
{
cout<<"\nThe details of the book are\n";
put();
cout<<"\nThe name of the publisher is:"<<pname;
cout<<"\nThe year of publication is:"<<yr;
}
};
void main()
{
clrscr();
moreinfo obj1;
obj1.getdetails();
obj1.showdetails();
getch();
}

output:
Enter the excession no.:3
Enter the name of the book:c++
Enter the author's name:sumita arora
Enter the cost of the book:250
Enter the name of the publisher:dhanpat rai & co.

Enter the year of publication:2004

The details of the book are


The excession no. of the book is:3
The name of the book is:ra
The name of the author is:sumita arora
The cost of the book is:250
The name of the publisher is:dhanpat rai & co.
The year of publication is:2004
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class item
{
int price,code;

public:
item()
{
price =0;
code =0;
}
item(int a,int b)
{
price =a;
code= b;
}
item(item &obj3)
{
price = obj3.price;
code= obj3.code;
}
void display()
{
cout<<price<<"\n"<<code<<"\n";
}
};
void main()
{
clrscr();
item obj1(70,80),obj2(40,50);
obj1.display();
obj2.display();
obj2 = obj1;
obj2.display();
getch();
}
//wap to construct 2 classes named employee with details-(name,address,company) &
second class
named perk with details-da(25% of salary),hra(20% of salary).implement to find the net
salary//

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
{
char e_name[30];
char address[40];
char company[20];
public:
void read()
{
cout<<"\nEnter the name of employee : ";
gets(e_name);
cout<<"\nEnter the address of employee : ";
gets(address);
cout<<"\nEnter the name of company : ";
gets(company);
}
void display()
{
cout<<"\n\nThe name of employee is : "<<e_name;
cout<<"\n\nThe address of the employee is : "<<address;
cout<<"\n\nThe name of the company is : "<<company;
}
};
class perk
{
float basic,da,hra,net_salary;
public:
void e_perk()
{
cout<<"\nEnter yhe basic salary : ";
cin>>basic;
da = 0.25 * basic;
cout<<"\n\nDa is : ";
cout<<da;
hra = 0.20 * basic;
cout<<"\n\nHra is : ";
cout<<hra;
}
void calc(employee obj1)
{
cout<<"\n\n----------------The details of the employee is ---------------- ";
obj1.display();
cout<<"\n\nThe Net Salary is : ";
net_salary = basic + da + hra;
cout<<net_salary;
}
};
void main()
{
employee obj1;
perk obj2;
clrscr();
obj1.read();
obj2.e_perk();
obj2.calc(obj1);
getch();
}

output:

Enter the name of employee : ramesh

Enter the address of employee : 29-mandakani,rohini

Enter the name of company : hcl

Enter yhe basic salary : 6000

Da is : 1500

Hra is : 1200

----------------The details of the employee is ----------------

The name of employee is : ramesh

The address of the employee is : 29-mandakani,rohini

The name of the company is : hcl


The Net Salary is : 8700
//wap to declare a class called matrix which is represented by 2-d array.write functions to
perform the
addition of 2 matrices & store them into 3rd matrix//

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class matrix
{
int a[2][2];

public:
void read();
void add(matrix,matrix);
};
void matrix::read()
{
cout<<"\nEnter the value of matrix :-----------------\n";
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cin>>a[i][j];
}
}
}
void matrix::add(matrix a1,matrix b1)
{
int i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
a[i][j] = a1.a[i][j] + b1.a[i][j];
}
}
cout<<"\nThe matrix after addition is :----------------\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[i][j]<<"\n";
}
}
}
void main()
{
clrscr();
matrix a,b,c;
a.read();
b.read();
c.add(a,b);
getch();
}

output:

Enter the value of matrix :-----------------


4
2
5
7

Enter the value of matrix :-----------------


2
4
6
7

The matrix after addition is :----------------


6
6
11
14
//wap to implement a class named student with following details :
-read()-to read i/p
-avg()-to cal. the average marks
-display()-to display the student details//

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class student
{
char st_name[20];
int r_no;
int m1,m2,m3,m4,m5;

public :
void read()
{
cout<<"\n\nenter your name : ";
gets(st_name);
cout<<"\nenter roll number : ";
cin>>r_no;
cout<<"\nenter marks of m1,m2,m3,m4,m5\n";
cin>>m1>>m2>>m3>>m4>>m5;
}

void display()
{
cout<<"\n\nThe name of student is : "<<st_name;
cout<<"\n\nthe roll number is : "<<r_no;
cout<<"\n\nthe marks are:
\n"<<m1<<"\n"<<m2<<"\n"<<m3<<"\n"<<m4<<"\n"<<m5;
}
void avg()
{
int avg;
avg = (m1+m2+m3+m4+m5)/5;
cout<<"\n\nThe average is : "<<avg;
}
};
void main()
{
student a;
clrscr();
a.read();
a.display();
a.avg();
getch();
}

output:

enter your name : ram

enter roll number : 12

enter marks of m1,m2,m3,m4,m5


70
60
78
90
88

The name of student is : ram

the roll number is : 12

the marks are:


70
60
78
90
88

The average is : 77
//wap to input & show the details(marks in 2 subjects & total)of student using concept of
inheritance//
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class student
{
protected:
int roll_no;

public:
void getroll_no()
{
cout<<"\nEnter Your Roll Number :";
cin>>roll_no;
}
void put()
{
cout<<"\nYour roll_no";
}
};
class test:public student
{
protected:
int sub1,sub2;

public:
void showsub();
void getsub()
{
cout<<"\nEnter the marks in two subject : ";
cin>>sub1>>sub2;
}
};
void test::showsub()
{
cout<<"\nSubject 1 marks is : "<<sub1;
cout<<"\nSubject 2 marks is : "<<sub2;
}
class result:public test
{
int total;
public:
void showdetail();
void getdetail()
{
getroll_no();
getsub();
}
};
void result::showdetail()
{
total = sub1 + sub2;
cout<<"\nThe details of the student is : "<<total;
showsub();
cout<<"\nThe result is : "<<total;
}
void main()
{
clrscr();
result std;
std.getdetail();
std.showdetail();
getch();
}

output:

Enter Your Roll Number :4

Enter the marks in two subject : 66


88

The details of the student is :


Subject 1 marks is : 66
Subject 2 marks is : 88
total marks: 154
//wap to input & show the details(marks in 2 subjects & total)of student using concept of
inheritance//
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class student
{
protected:
int roll_no;

public:
void getroll_no()
{
cout<<"\nEnter Your Roll Number :";
cin>>roll_no;
}
void put()
{
cout<<"\nYour roll_no";
}
};
class test:public student
{
protected:
int sub1,sub2;

public:
void showsub();
void getsub()
{
cout<<"\nEnter the marks in two subject : ";
cin>>sub1>>sub2;
}
};
void test::showsub()
{
cout<<"\nSubject 1 marks is : "<<sub1;
cout<<"\nSubject 2 marks is : "<<sub2;
}
class result:public test
{
int total;
public:
void showdetail();
void getdetail()
{
getroll_no();
getsub();
}
};
void result::showdetail()
{
total = sub1 + sub2;
cout<<"\nThe details of the student is : "<<total;
showsub();
cout<<"\nThe result is : "<<total;
}
void main()
{
clrscr();
result std;
std.getdetail();
std.showdetail();
getch();
}

output:

Enter Your Roll Number :4

Enter the marks in two subject : 66


88

The details of the student is :


Subject 1 marks is : 66
Subject 2 marks is : 88
total marks: 154
//wap to illustrate the working of function-overloading

//

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int compute(int,int);
void compute(int,int,int);
void compute(char);
void main()
{
int l,b,h,a;
clrscr();
cout<<"\nEnter the length :";
cin>>l;
cout<<"\nEnter the Breadth :";
cin>>b;
cout<<"\nEnter the Height : ";
cin>>h;
a = compute(l,b);
cout<<"\nThe Area is : " <<a<<"\n";
compute('*');
compute(l,b,h);
compute('*');
getch();
}
void compute(char c)
{
for(int i=0;i<10;i++)
cout<<c;
}
int compute(int l1,int b1)
{
int area;
area = l1 * b1;
return(area);
}
void compute(int l1,int b1,int h1)
{
int vol;
vol =l1 * b1 * h1;
cout<<"\nThe volume is : "<<vol<<"\n";
}
output:
Enter the length :2

Enter the Breadth :3

Enter the Height : 4

The Area is : 6
**********
The volume is : 24
**********

You might also like