gtu practical solution ashish b. prajapati a class called item having data members item_code,...

35
Ashishprajapati29.wordpress.com 2017 Ashish B. Prajapati (9879578201) Page 1 GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI 1. Create a class Account. It has three data member account id, name and balance. Define function to assign value and display value. Define function that search account number given by the user. If account number exists, print detail of that account. Write a program using array of object. Declare at least 5 account and print details. #include<iostream> #include<stdio.h> using namespace std; class account { int id,balance; char name[20]; public: void getdata() { cout<<"Enter id = "; cin>>id; cout<<"Enter name = "; cin>>name; cout<<"Enter balance = "; cin>>balance; } void display() { cout<<"\nId = "<<id<<endl; cout<<"Name = "<<name<<endl; cout<<"Balance = "<<balance<<endl; } void search(account *a) { int n,flag=0; cout<<"Enter id for search = "; cin>>n; for(int i=0;i<n;i++) { if(a[i].id == n)

Upload: lamquynh

Post on 16-Mar-2018

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 1

GTU PRACTICAL SOLUTION

ASHISH B. PRAJAPATI

1. Create a class Account. It has three data member account id, name and

balance. Define function to assign value and display value. Define

function that search account number given by the user. If account

number exists, print detail of that account. Write a program using array

of object. Declare at least 5 account and print details.

#include<iostream>

#include<stdio.h>

using namespace std;

class account

{

int id,balance;

char name[20];

public:

void getdata()

{

cout<<"Enter id = ";

cin>>id;

cout<<"Enter name = ";

cin>>name;

cout<<"Enter balance = ";

cin>>balance;

}

void display()

{

cout<<"\nId = "<<id<<endl;

cout<<"Name = "<<name<<endl;

cout<<"Balance = "<<balance<<endl;

}

void search(account *a)

{

int n,flag=0;

cout<<"Enter id for search = ";

cin>>n;

for(int i=0;i<n;i++)

{

if(a[i].id == n)

Page 2: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 2

{

a[i].display();

flag = 0;

}

}

if(flag==0)

{

cout<<"Successfully done!!!"<<endl;

}

else

{

cout<<"Data not found"<<endl;

}

}

};

int main()

{

account a[3];

for(int i=0;i<3;i++)

{

a[i].getdata();

}

account temp;

temp.search(a);

}

Output:

Page 3: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 3

2. Define a class Time with hours and minutes as two data members, add

necessary member functions to initialize and display data of class. Do not

use constructors in a class. Define a member function sum () which adds

two Time objects. Invoke the statements like T3.sum (T1, T2) in main ().

#include<iostream>

#include<stdio.h>

using namespace std;

class time

{

int hr,min;

public:

void get_time()

{

cout<<"Enter hours = ";

cin>>hr;

cout<<"Enter minutes = ";

cin>>min;

}

void display()

{

if(min >= 60)

{

int temp;

temp = min/60;

hr = hr + temp;

min = min % 60;

}

cout<<"Hours = "<<hr<<endl;

cout<<"Minutes = "<<min<<endl;

}

void sum(time t1,time t2)

{

hr = t1.hr + t2.hr;

min = t1.min + t2.min;

}

};

int main()

{

time t1,t2,t3;

Page 4: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 4

t1.get_time();

t2.get_time();

t3.sum(t1,t2);

t3.display();

}

Output:

3. Define a class complex with real and imaginary as two data member, add

necessary constructors and member function to initialize and display

data of class. Class should overload the + operator to add two complex

objects and return the results. Invoke the statements like C3=C1+C2 in

main ().

#include<iostream>

using namespace std;

class complex

{

float img,real;

public:

complex()

{

real = 0.0;

img = 0.0;

}

void getdata()

{

cout<<"Enter real value = ";

cin>>real;

cout<<"Enter img value = ";

cin>>img;

Page 5: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 5

}

void setdata(float r,float i)

{

real = r;

img = i;

}

void display()

{

cout<<real<<" + "<<img<<"i"<<endl;

}

complex operator + (complex c)

{

complex temp;

temp.real = real + c.real;

temp.img = img + c.img;

return temp;

}

};

int main()

{

complex c1,c2,c3;

c1.getdata();

c2.setdata(2,4.5);

cout<<"Complex - 1 : ";

c1.display();

cout<<"Complex - 2 : ";

c2.display();

c3 = c1 + c2;

cout<<"Addition : ";

c3.display();

}

Output:

Page 6: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 6

4. Define Operator overloading. Create class Time that has three data

members hour, minute and second and two constructor, default

constructor and parameterized constructor to initialize data member.

Write a program to add two times by overloading operator +.

#include<iostream>

using namespace std;

class time

{

int hr,min,sec;

public:

time()

{

hr,min,sec=0;

}

time(int h,int m,int s)

{

hr = h;

min = m;

sec = s;

}

void gettime()

{

cout<<"Enter time in (h m s) format : ";

cin>>hr>>min>>sec;

}

void display()

{

cout<<hr<<" : "<<min<<" : "<<sec<<endl;

}

time operator + (time t)

{

time temp;

temp.hr = hr + t.hr;

temp.min = min + t.min;

temp.sec = sec + t.sec;

if(temp.sec>=60)

{

int n = temp.sec/60;

temp.min = temp.min + n;

temp.sec = temp.sec%60;

Page 7: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 7

}

if(temp.min>=60)

{

int n= temp.min/60;

temp.hr = temp.hr + n;

temp.min = temp.min%60;

}

return temp;

}

};

int main()

{

time t1,t2(2,45,90),t3;

t1.gettime();

cout<<"\nTime 1 : \n\n";

t1.display();

cout<<"\nTime 2 : \n\n";

t2.display();

t3 = t1 + t2;

cout<<"\nAddition : \n\n";

t3.display();

}

Output:

Page 8: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 8

5. Explain Friend function and its characteristics. Define a class matrix

with an integer array of 3X3 as a data member. Define a friend function

which adds two matrix objects and returns resultant matrix object.

#include<iostream>

using namespace std;

class mat_B;

class mat_A

{

int A[3][3];

public:

mat_A()

{

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

A[i][j] = 0;

}

}

}

void get_A()

{

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

cout<<"\nEnter value at ["<<i<<"]["<<j<<"] = ";

cin>>A[i][j];

}

}

}

friend void add(mat_A a1,mat_B b1);

};

class mat_B

{

int B[3][3];

public:

mat_B()

{

Page 9: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 9

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

B[i][j] = 0;

}

}

}

void get_B()

{

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

cout<<"\nEnter value at ["<<i<<"]["<<j<<"] = ";

cin>>B[i][j];

}

}

}

friend void add(mat_A a1,mat_B b1);

};

void add(mat_A a1,mat_B b1)

{

int C[3][3];

for(int i=0;i<3;i++)

{

for(int j=0;j<3;j++)

{

C[i][j] = a1.A[i][j] + b1.B[i][j];

cout<<C[i][j]<<"\t";

}

cout<<endl;

}

}

int main()

{

mat_A m1;

mat_B m2;

cout<<"Enter value in matrix - 1"<<endl<<endl;

m1.get_A();

Page 10: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 10

cout<<"\nEnter value in matrix - 2"<<endl<<endl;

m2.get_B();

cout<<"\nAddition of 2 matrix is "<<endl<<endl;

add(m1,m2);

}

Output:

6. Assume that Circle is defined using radius and Cylinder is defined using

radius and height. Write a Circle class as base class and inherit the

Cylinder class from it. Develop classes such that user can compute the

area of Circle objects and volume of Cylinder objects. Area of Circle is

pie *radius*radius, while volume of Cylinder is pie*(radius *

radius)*height.

#include<iostream>

using namespace std;

class circle

{

protected:

float r,area;

Page 11: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 11

public:

circle()

{

r=0;

}

void get_radius()

{

cout<<"Enter Radius = ";

cin>>r;

}

void area_circle()

{

area = 3.14 * r * r;

}

void display_area()

{

cout<<endl<<"Circle area = "<<area<<endl;

}

};

class cylinder : public circle

{

float volume,h;

public:

void volume_cylinder()

{

get_radius();

area_circle();

cout<<"Enter height = ";

cin>>h;

volume = 3.14 * (r * r) * h;

}

void display_vol()

{

display_area();

cout<<"Volume of cylinder = "<<volume;

}

};

int main()

{

cylinder c;

Page 12: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 12

c.volume_cylinder();

c.display_vol();

}

Output:

7. Declare a class called book having data members like book_title,

publisher, and author_name. Overload extractor and inserter operator

(>> and <<) for class book.

#include<iostream>

using namespace std;

class book

{

char book_title[30],publisher[30],author_name[30];

public:

friend istream & operator >> (istream &din, book &obj);

friend ostream & operator << (ostream &dout, book &obj);

};

istream & operator >> (istream &din, book &obj)

{

cout<<"\nEnter Data for Book\n";

cout<<"Enter book_title : ";

din>>obj.book_title;

cout<<"Enter publisher : ";

din>>obj.publisher;

cout<<"Enter author name : ";

din>>obj.author_name;

return(din);

}

ostream & operator << (ostream &dout, book &obj)

{

dout<<"\nBook details are = \n";

Page 13: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 13

dout<<"\nBook Title :"<<obj.book_title;

dout<<"\nPublisher :"<<obj.publisher;

dout<<"\nAuthor name :"<<obj.author_name;

return(dout);

}

int main()

{

book b;

cin>>b;

cout<<b;

}

Output:

8. Write a program to copy the contents of a source file student1.txt to a

destination file student2.txt character by character.

#include<iostream>

#include<fstream>

using namespace std;

int main()

{

ofstream fout1,fout2;

fout1.open("D:\\file1.txt");

fout2.open("D:\\file2.txt");

fout1<<"Hello my name is ashish prajapati";

fout1.close();

ifstream fin;

Page 14: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 14

fin.open("D:\\file1.txt",ios::out);

char ch = fin.get();

while(!fin.eof())

{

cout<<ch;

fout2<<ch;

ch = fin.get();

}

cout<<endl<<"Successfully Done!!!!";

fout2.close();

fin.close();

}

Output:

9. Write a program to find the sum of two private data members x and y of

class A and B using a common friend function. Assume that the

prototype for both the classes will be void sum (A,B);

#include<iostream>

using namespace std;

class B;

class A

{

int x;

public:

void getdata()

{

cout<<"Enter value of X = ";

cin>>x;

}

friend void sum(A,B);

};

Page 15: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 15

class B

{

int y;

public:

void getdata()

{

cout<<"Enter value of Y = ";

cin>>y;

}

friend void sum(A,B);

};

void sum(A a,B b)

{

int c = a.x + b.y;

cout<<"Ans = "<<c;

}

int main()

{

A a;

B b;

a.getdata();

b.getdata();

sum(a,b);

}

Output:

10. Declare a class called book_details to represent details of book having

data members like title, author, edition, price and

no_of_copies_available. Define following functions:

Constructors.

Display data.

Page 16: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 16

Find_books to find and display details of all books having price

greater than Rs. 250.

#include<iostream>

using namespace std;

class book_details

{

char title[20], author[20], edition[5];

int price ,no_of_copies_available;

public:

book_details()

{

price = 0;

no_of_copies_available = 0;

}

void getdata()

{

cout<<"Enter Title = ";

cin>>title;

cout<<"Enter author = ";

cin>>author;

cout<<"Enter edition = ";

cin>>edition;

cout<<"Enter price = ";

cin>>price;

cout<<"Enter no of copies available = ";

cin>>no_of_copies_available;

}

void display()

{

cout<<"\n******************************"<<endl;

cout<<"Title = "<<title<<endl;

cout<<"Author = "<<author<<endl;

cout<<"Edition = "<<edition<<endl;

cout<<"Price = "<<price<<endl; cout<<"No of copies available = "<<no_of_copies_available<<endl;

}

void search(book_details *b)

{

for(int i=0;i<3;i++)

{

Page 17: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 17

if(b[i].price>=250)

{

b[i].display();

}

}

}

};

int main()

{

book_details b[3];

for(int i=0;i<3;i++)

{

b[i].getdata();

}

cout<<"\nDetails of all books having price greater than Rs. 250."<<endl;

b[0].search(b);

}

Output:

11. Declare a class called item having data members item_code, item_name,

cost and discount. Derive two classes from class item, namely employee

and customer. The employee class has data members like emp_code,

Page 18: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 18

emp_name and amount. The class customer has data members like

cus_name and amount. Define following functions for.

Display data.

Computing amount to be paid for a purchased item.

Also define function main to create objects of both derived classes and to

show usage of above functions.

#include<iostream>

using namespace std;

class item

{

protected:

int item_code,cost;

float discount;

char item_name[20];

public:

void get_item()

{

cout<<"Enter item code = ";

cin>>item_code;

cout<<"Enter item name = ";

cin>>item_name;

cout<<"Enter total cost = ";

cin>>cost;

cout<<"Enter discount (Percentage) = ";

cin>>discount;

}

void display_item()

{

cout<<"******************************"<<endl;

cout<<"Item code = "<<item_code<<endl;

cout<<"Item name = "<<item_name<<endl;

cout<<"Item cost = "<<cost<<endl;

}

float disc()

{

return (discount/100);

}

};

class employee : public item

Page 19: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 19

{

char emp_name[20];

float amount;

public:

void get_emp()

{

get_item();

cout<<"Enter employee name = ";

cin>>emp_name;

}

void disp_emp()

{

display_item();

cout<<"Employee name = "<<emp_name<<endl;

cout<<"Total payable amount with "<<disc()<<"% for

"<<emp_name<<" is = "<<get_amount()<<endl;

cout<<"******************************"<<endl;

}

float get_amount()

{

return (cost - (cost*disc()));

}

};

class customer : public item

{

char c_name[20];

float amount;

public:

void get_customer()

{

get_item();

cout<<"Enter customer name = ";

cin>>c_name;

}

void disp_customer()

{

display_item();

cout<<"customer name = "<<c_name<<endl;

cout<<"Total payable amount with "<<disc()<<" for

"<<c_name<<" is = "<<get_amount()<<endl;

}

Page 20: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 20

float get_amount()

{

return (cost - (cost*disc()));

}

};

int main()

{

employee e;

customer c;

cout<<"Enter data for employee"<<endl;

e.get_emp();

e.disp_emp();

cout<<"\n\nEnter data for customer"<<endl;

c.get_customer();

c.disp_customer();

}

Output:

12. Declare a class called bird having private data members name and

weight. Define following functions for.

Default constructor for reading data members from key board.

Overloaded constructor with two arguments to be used for

initialization of data members.

Display function to display data members.

Page 21: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 21

Overloaded member operator >= to compare weight of two birds,

returning false if weight of first bird object is less than that of the

second & true otherwise.

Define main to illustrate use of above functions.

#include<iostream>

#include<string.h>

using namespace std;

class bird

{

public:

char name[20];

int weight;

bird()

{

cout<<"Enter bird name = ";

gets(name);

cout<<"Enter bird weight = ";

cin>>weight;

}

bird(char n[],int w)

{

strcpy(name,n);

weight = w;

}

int operator >=(bird b)

{

if(weight>=b.weight)

{

return 1;

}

else

{

return 0;

}

}

void display()

{

cout<<"\n******************************"<<endl;

cout<<"\nweight of "<<name<<" is "<<weight<<endl;

}

};

Page 22: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 22

int main()

{

bird b1,b2("sparrow",23);

b1.display();

b2.display();

cout<<"\n******************************"<<endl;

if(b1>=b2)

{

cout<<"\nWeight of "<<b1.name<<" is larger than

"<<b2.name<<endl;

}

else

{

cout<<"\nWeight of "<<b2.name<<" is larger than

"<<b1.name<<endl;

}

cout<<"\n******************************"<<endl;

}

Output:

13. Declare a template class called exam having an array of generic type as a

data member, named elements [10]. Define following generic (template)

member functions:

Sort to arrange elements in ascending order

Find_max to find and return maximum from the array

Define main to illustrate usage of these functions to process two different

types of data.

include<iostream>

Page 23: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 23

#include<iomanip>

using namespace std;

template <class t>

class exam

{

t element[10];

public:

void get(int n)

{

int i;

cout<<"Enter the array elements: ";

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

cin>>element[i];

}

void sort(int n)

{

int i,j;

t temp;

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

{

for(j=i+1;j<n;j++)

{

if(element[i]>element[j])

{

temp=element[i];

element[i]=element[j];

element[j]=temp;

}

}

}

}

void display(int n)

{

int i;

cout<<"\nThe sorted array is : "<<endl;

cout<<"\n***************\n\n";

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

{

cout<<element[i]<<"\t";

}

cout<<"\n\n***************\n";

Page 24: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 24

}

void find_max(int n)

{

int max = element[0];

for(int i=0;i<n;i++)

{

if(max<element[i])

{

max = element[i];

}

}

cout<<"\nMax = "<<max;

}

};

int main()

{

int n,ch;

exam<int> b1;

cout<<"Enter the size of array: ";

cin>>n;

b1.get(n);

b1.sort(n);

b1.display(n);

b1.find_max(n);

}

Output:

Page 25: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 25

14. Declare a class logic_gates to represent logic gates. The class has three

data members a,b and c. The class have one virtual function

get_gate_output. The class has two derived class namely and_gate and

or_gate. Define function get_gate_output in both these classes to get the

output of the gate. Show the use of above classes and function to

demonstrate dynamic polymorphism in function main.

#include <iostream>

using namespace std;

class logic_gates

{

protected:

int a,b,c;

public:

void get_input()

{

cout<<"Enter 3 input in binary ( 0 and 1 ) = ";

cin>>a>>b>>c;

}

virtual void get_gate_output(){}

};

class and_gate : public logic_gates

{

int temp;

public:

void get_gate_output()

{

get_input();

if((a==0 && b==1)||(a==1 && b==0))

{

temp = 0;

if(temp==0 && c==0)

{

temp = 0;

}

else if(temp==0 && c==1)

{

temp = 0;

}

}

Page 26: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 26

else if(a==1 && b==1)

{

temp = 1;

if(temp==1 && c==0)

{

temp = 0;

}

else if(temp==1 && c==1)

{

temp = 1;

}

}

cout<<"\n\nAND gate ans = "<<temp<<endl;

}

};

class or_gate : public logic_gates

{

int te;

public:

void get_gate_output()

{

get_input();

if((a==0 && b==1) || (a==1 && b==0))

{

te=1;

if(te==1 && c==0)

{

te = 0;

}

else if(te==1 && c==1)

{

te = 1;

}

}

else if(a==0 && b==0)

{

te=0;

if(te==0 && c==0)

Page 27: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 27

{

te=0;

}

else if(te==0 && c==1)

{

te=1;

}

}

else if(a==1 && b==1)

{

te = 1;

if((te==1 && c==0) || (te==1 && c==1))

{

te=1;

}

}

cout<<"\n\nOR gate ans = "<<te;

}

};

int main()

{

logic_gates *lg;

and_gate ag;

or_gate og;

lg = &ag;

lg->get_gate_output();

lg = &og;

lg->get_gate_output();

}

Output:

Page 28: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 28

15. Create a class coordinate containing x, y and z private data members.

Perform operations for incrementing, adding and comparing objects by

overloading ++, += and = = operators respectively. Define necessary

functions to set and display the variables.

#include <iostream>

using namespace std;

class coordinate

{

int x,y,z;

public:

coordinate()

{

x=y=z=0;

}

void setdata(int a,int b,int c)

{

x = a;

y = b;

z = c;

}

void display()

{

cout<<"X = "<<x<<" Y = "<<y<<" Z = "<<z<<endl;

}

void operator ++()

{

x++;

y++;

z++;

}

void operator +=(coordinate c)

{

coordinate temp;

temp.x = x + c.x;

temp.y = y + c.y;

temp.z = z + c.z;

cout<<"****************************************************

*****"<<endl;

cout<<"Addition of two object : ";

Page 29: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 29

temp.display();

}

int operator ==(coordinate c)

{

if(x==c.x && y==c.y && z==c.z)

{

return 1;

}

else

{

return 0;

}

}

};

int main()

{

coordinate c1,c2;

c1.setdata(1,2,3);

cout<<"Object - 1 : ";

c1.display();

cout<<"****************************************************

*****"<<endl;

c2.setdata(5,2,3);

cout<<"Object - 2 : ";

c2.display();

cout<<"****************************************************

*****"<<endl;

++c1;

cout<<"Object 1 after increment : ";

c1.display();

++c2;

cout<<"****************************************************

*****"<<endl;

cout<<"Object 2 after increment : ";

c2.display();

c1+=c2;

if(c1==c2)

{

cout<<"****************************************************

*****";

Page 30: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 30

cout<<"\nBoth are same object";

}

else

{

cout<<"****************************************************

*****";

cout<<"\nBoth are not same object";

}

}

Output:

16. Write a program to demonstrate conversion of an object of one class into

an object of another class.

OR

WAP to convert from ton to Kg & gms and vice-versa. Create two classes

for the same. Class tons (with ton as data member) and Class kilo (with

kg and gms as data members) Use formula 1 ton = 100 kg, 1kg =

1000gms.

#include<iostream>

#include<conio.h>

using namespace std;

class kilo;

class tons

{

public:

int tns,tns1;

void getdata()

{

cout<<"Enter value in tons = ";

Page 31: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 31

cin>>tns;

}

int ret_tns()

{

return tns;

}

operator kilo();

void display()

{

cout<<"Tns 1 = "<<tns<<endl;

cout<<"Tns 2 = "<<tns1;

}

};

class kilo

{

public:

int kg,grm;

void getinput()

{

cout<<"\nEnter kg and grm values = ";

cin>>kg>>grm;

}

void display()

{

cout<<kg<<" "<<grm;

}

int ret_kg()

{

return kg;

}

int ret_grm()

{

return grm;

}

operator tons();

};

tons :: operator kilo()

{

kilo k1;

k1.kg = ret_tns() * 100;

k1.grm = ret_tns() * 1000;

Page 32: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 32

return k1;

}

kilo :: operator tons()

{

tons t;

t.tns = ret_kg() / 100;

t.tns1 = ret_grm() / 1000;

return t;

}

int main()

{

tons t;

t.getdata();

kilo k;

k=t;

k.display();

k.getinput();

t = k;

t.display();

getch();

}

Output:

17. Write a program to copy the contents of file A.txt into another B.txt by

reversing case of the character. E.g. input in file A.txt is aBcD and

output in file B.txt is AbCd.

#include<iostream>

#include<ctype.h>

#include<string.h>

#include<fstream>

using namespace std;

Page 33: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 33

int main()

{

ofstream fout1,fout2;

fout1.open("string1.txt");

fout2.open("string2.txt");

char str[20];

cout<<"Enter string = ";

cin.get(str,20);

fout1<<str;

fout1.close();

ifstream fin;

fin.open("string1.txt");

char ch = fin.get();

while(!fin.eof())

{

if(ch>='a' && ch<='z')

{

char t = toupper(ch);

fout2<<t;

cout<<t;

ch = fin.get();

}

else

{

char t = tolower(ch);

fout2<<t;

cout<<t;

ch = fin.get();

}

}

fin.close();

fout2.close();

}

Output:

Page 34: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 34

18. Create a generic class stack using template and implement common Push

and Pop operations for different data types.

#include<iostream>

using namespace std;

template<class T>

class stack

{

T* arr;

static int top;

public:

stack(int);

void push(T);

T pop();

bool empty();

};

template<class T>

int stack<T>::top = -1;

template<class T>

stack<T>::stack(int x)

{

arr = new T(x);

}

template<class T>

void stack<T>::push(T a)

{

arr[++top] = a;

}

template<class T>

T stack<T>::pop()

{

T a = arr[top--];

return a;

}

template<class T>

bool stack<T>::empty()

Page 35: GTU PRACTICAL SOLUTION ASHISH B. PRAJAPATI a class called item having data members item_code, item_name, ... int item_code,cost; float discount; char item_name[20]; public: void get_item()

Ashishprajapati29.wordpress.com 2017

Ashish B. Prajapati (9879578201) Page 35

{

return (top==-1);

}

int main()

{

stack<int> s(10);

int n;

int i;

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

{

cout<<"Enter element value = ";

cin>>n;

s.push(n);

}

cout<<"\nElement values are : \n"<<endl;

while(!s.empty())

{

cout<<s.pop()<<"\t";

}

return 0;

}

Output: