Data Member, functions and class Diagrams
Task 1:
Define a Class BOOK with the following specifications: Data Members: bookNo
bookTitle price Member Functions: take_data( ) A function to input bookNo,
bookTitle, price. purchase_info( ) A function to ask the user to input the number
of copies to be purchased of a book. Then multiply it with price of the book.
[Note: User can search the book using its bookNo] Write a menu driven
program. Get data for five book from user.
#include<iostream>
#include<string>
using namespace std;
class book{
int bookno;
string booktitle;
int price=200;
public:
void takedata();
void purchaseinfo();
void purchaseid();
void user();
void admin();
void show();
void amain();
void showf();
int setp(){
return price;
}
int setid(){
return bookno;
}
string settitle()
{
return booktitle;
}
};
book b[5];
int main()
{
book b;
int a;
cout << "Enter 1 for user and 2 for admin" << endl;
cout << "User" << endl;
cout << "Admin" << endl;
cin >> a;
if (a == 1)
{
b.user();
}
if (a == 2)
{
b.admin();
}
system("pause");
getchar();
getchar();
return 0;
}
void book::takedata()
{
cout << "Enter the book number:";
cin >> bookno;
cout << "Enter the book title:";
cin.ignore();
getline(cin, booktitle);
}
void book::purchaseinfo()
{
int copy;
cout << "How many copies of book u want:";
cin >> copy;
price= price*copy;
}
void book::user()
{
for (int i = 0; i < 5; i++)
{
cout << "_________________________" << endl;
b[i].purchaseinfo();
b[i].show();
}
for (int i = 0; i < 5; i++)
{
cout << "-------------------------------" << endl;
cout << "Your price is:" << b[i].setp() << endl;
}
}
void book::showf()
{
for (int i = 0; i < 5; i++)
{
cout << "Book id is:" << b[i].bookno << endl;
cout << "Book name is:" << b[i].booktitle << endl;
cout << "Book price is:" << b[i].price << endl;
}
}
void book::show()
{
int id;
cout << "Enter the id:";
cin >> id;
for (int i = 0; i < 5; i++)
{
if (id == b[i].setid())
{
cout << "Book id is:" << b[i].setid() << endl;
cout << "Book name is:" << b[i].settitle() << endl;
cout << "Book price is:" << b[i].setp() << endl;
}
}
}
void book::admin()
{
for (int i = 0; i < 5; i++)
{
b[i].takedata();
b[i].purchaseinfo();
}
for (int i = 0; i < 5; i++)
{
b[i].showf();
}
int ag;
cout << "Enter 1 to go in main:";
cin >> ag;
if (ag == 1)
{
amain();
}
}
void book::amain()
{
main();
}
Task2:
Implement the given class diagram.
Test
-a:int
-b-int +
setdata(int,int):
void +getdata(Test t[ ]):void
#include<iostream>
using namespace std;
class Test{
int a;
int b;
public:
void setdata(int x, int y);
void getdata(Test[]);
};
int main()
{
Test b, t[3];
b.getdata(t);
getchar();
getchar();
return 0;
}
void Test::setdata(int x,int y)
{
a = x; b = y;
}
void Test::getdata(Test t[])
{
int x, y;
Test z;
for (int i = 0; i < 3; i++)
{
cout << "Enter the value for x:";
cin >> x;
cout << "Enter the value for y:";
cin >> y;
t[i].setdata(x, y);
}
for (int i = 0; i < 3; i++)
{
cout << "Value is:" << t[i].a << ":" <<t[i].b << endl;
}
}
Task 3:
Write a program to add, subtract and multiply two complex numbers
using class object to function.
#include<iostream>
using namespace std;
class Comp{
int r1, im1, r2, im2, temp1, temp2;
public:
Comp(){
r1 = im1 = r2 = im2 = 0;
}
Comp(int a, int b, int c, int d)
{
r1 = a; im1 = b;
r2 = c; im2 = d;
}
void input();
void out();
void addition();
void subtraction();
void Multiply();
};
Comp t;
int main()
{
t.input();
getchar();
getchar();
return 0;
}
void Comp::input()
{
int a, b, c, d;
cout << "Enter the real number 1:";
cin >> a;
cout << "Enter the imainary number 1:";
cin >> b;
cout << "Enter the real number 2:";
cin >> c;
cout << "Enter the imaginary number 2:";
cin >> d;
Comp t(a, b, c, d);
t.out();
t.addition();
t.subtraction();
t.Multiply();
}
void Comp::out()
{
cout << "Your complex number 1 is:" << r1 << " + " << im1 << "i" << endl;
cout << "Your complex number 2 is:" << r2 << " + " << im2 << "i" << endl;
}
void Comp::addition()
{
temp1 = r1 + r2;
temp2 = im1 + im2;
cout << "Your complex number with addition is:" << temp1 << " + " << temp2 <<"i"<<
endl;
}
void Comp::subtraction()
{
if (r1 > r2)
{
temp1 = r1 - r2;
}
else
{
temp1 = r2 - r1;
}
if (im1 > im2)
{
temp2 = im1 - im2;
}
else
{
temp2 = im2 - im1;
}
cout << "Your complex number with subtraction is:" << temp1 << " + " << temp2<<"i" <<
endl;
}
void Comp::Multiply()
{
temp1 = r1*r2;
temp2 = im1*im2;
cout << "Your Complex number after multiplication is:" << temp1 << " + " << temp2 <<
"i" << endl;
}
Comments
Post a Comment