Pratik Bakane C++

42
/*Program to declare class Account having data members as Account No. and balance. Accept this data for 3 accounts and give interest of 10% where balance is equal or greater than 5000 and display them*/ #include<iostream.h> #include<conio.h> class Account { public: int acc_no,bal,inte; void accept(); void display(); }; void Account::accept(void) { cout<<"\tEnter the account number : "; cin>>acc_no; cout<<"\tEnter the balance : "; cin>>bal; } void Account::display(void) {

Transcript of Pratik Bakane C++

Page 1: Pratik Bakane C++

/*Program to declare class Account having data members as Account No.

and balance. Accept this data for 3 accounts and give interest of 10% where

balance is equal or greater than 5000 and display them*/

#include<iostream.h>

#include<conio.h>

class Account

{

public:

int acc_no,bal,inte;

void accept();

void display();

};

void Account::accept(void)

{

cout<<"\tEnter the account number : ";

cin>>acc_no;

cout<<"\tEnter the balance : ";

cin>>bal;

}

void Account::display(void)

{

Page 2: Pratik Bakane C++

cout<<"\tThe account number is : "<<acc_no<<endl;

cout<<"\tThe balance is : "<<bal<<endl;

}

void main()

{

int i;

clrscr();

Account a[3];

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

{

a[i].accept();

}

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

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

{

if(a[i].bal>=5000)

{

a[i].inte=(a[i].bal*100)/10;

Page 3: Pratik Bakane C++

a[i].bal=a[i].bal+a[i].inte;

}

a[i].display();

cout<<"=============================================="<<endl;

}

getch();

}

Page 4: Pratik Bakane C++

OUTPUT

Page 5: Pratik Bakane C++

/*Program to declare a class city having data members as name and

population.Accept this data for 5 cities and display names of city

having highest population*/

#include<iostream.h>

#include<conio.h>

class city

{

public:

char name[20];

int population;

void accept()

{

cout<<"\tEnter the Name of the city: ";

cin>>name;

cout<<"\tEnter the population : ";

cin>>population;

}

void display()

{

cout<<"\tName is : "<<name<<endl;

Page 6: Pratik Bakane C++

cout<<"\tPopulation is : "<<population<<endl;

}

};

void main()

{

int i;

clrscr();

city c[5];

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

{

c[i].accept();

}

//sorting

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

{

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

{

if(c[j].population<c[j+1].population)

{

city temp = c[j];

Page 7: Pratik Bakane C++

c[j]=c[j+1];

c[j+1]=temp;

}

}

}

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

{

c[i].display();

}

getch();

}

Page 8: Pratik Bakane C++

OUTPUT

Page 9: Pratik Bakane C++

/*Program for swapping contents using friend function*/

#include<iostream.h>

#include<conio.h>

class sample

{

private:

int x,y;

public:

void setdata(int a,int b)

{

x=a;

y=b;

}

void showdata()

{

cout<<"\n\n\t\tx="<<x<<"\n\n\t\ty="<<y;

}

friend void swap(sample &s);

};

void swap(sample &s)

{

int temp;

temp=s.x;

Page 10: Pratik Bakane C++

s.x=s.y;

s.y=temp;

}

void main()

{

sample s;

int x1,x2;

clrscr();

cout<<"\n\n\t\tEnter 2 Numbers:";

cin>>x1>>x2;

s.setdata(x1,x2);

cout<<"\n\n\t\tBefore Swapping\n";

s.showdata();

cout<<"\n\n\t\tAfter Swapping\n";

swap(s);

s.showdata();

getch();

}

Page 11: Pratik Bakane C++

OUTPUT

Page 12: Pratik Bakane C++

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

class emp

{

public:

int emp_no,sal;

char ch;

void accept()

{

cout<<"\n\n\t\tEnter the number : ";

cin>>emp_no;

cout<<"\n\n\t\tEnter the salary : ";

cin>>sal;

}

void display()

{

cout<<"\n\n\t\tEnter another(Y/N) ? : ";

// cin>>ch;

}

};

Page 13: Pratik Bakane C++

void main()

{

char ch;

emp e,e1;

clrscr();

e.accept();

e.display();

// cout<<"Enter u r choice Y|N : ";

cin>>ch;

if(ch=='y' || ch=='Y')

{

e.accept();

e1.accept();

}

else

{

cout<<"\n\n\t\tExit";

}

getch();

}

Page 14: Pratik Bakane C++

OUTPUT :

Page 15: Pratik Bakane C++

/**/

#include<iostream.h>

#include<conio.h>

class sample

{

static int r;

int p,d,i;

public:

void accept();

void display();

};

int sample::r;

void sample::accept()

{

cout<<"\n\n\t\tEnter principal : ";

cin>>p;

cout<<"\n\n\t\tEnter the rate of interest : ";

cin>>r;

cout<<"\n\n\t\tEnter the duration : ";

cin>>d;

}

Page 16: Pratik Bakane C++

void sample::display()

{

i=p*d*r/100;

cout<<"\n\n\t\tSimple Interest : "<<i;

}

void main()

{

clrscr();

sample s;

s.accept();

s.display();

getch();

}

Page 17: Pratik Bakane C++

OUTPUT :

Page 18: Pratik Bakane C++

#include<iostream.h>

#include<conio.h>

class base1

{

private:

int b1;

};

class base2

{

private:

int b2;

};

class derived:public base1,public base2

{

private:

int d1;

};

void main()

{

clrscr();

cout<<endl<<"\n\n\t\t"<<sizeof(base1)<<endl<<"\n\n\t\t"

<<sizeof(base2)<<endl<<"\n\n\t\t"<<sizeof(derived);

Page 19: Pratik Bakane C++

getch();

}

OUTPUT:

Page 20: Pratik Bakane C++

/*Program to implement multiple inheritance. Assume suitable

member variables and member functions(using virtual base class*/

#include<iostream.h>

#include<conio.h>

class acc

{

private:

char name[20];

public:

void accept1()

{

cout<<"\n\n\t\tEnter the name : ";

cin>>name;

}

void display1()

{

cout<<"\n\n\t\tThe name is : "<<name;

}

};

class sav_a:virtual public acc

Page 21: Pratik Bakane C++

{

private:

int no;

public:

void accept2()

{

cout<<"\n\n\t\tEnter the number : ";

cin>>no;

}

void display2()

{

cout<<"\n\n\t\tThe number is : "<<no;

}

};

class cur_a:public virtual acc

{

private:

int a_no;

public:

void accept3()

{

cout<<"\n\n\t\tEnter the a_no number : ";

Page 22: Pratik Bakane C++

cin>>a_no;

}

void display3()

{

cout<<"\n\n\t\tThe a_no is : "<<a_no;

}

};

class fix_dip:public sav_a,public cur_a

{

private:

int rec_no;

public:

void accept4()

{

cout<<"\n\n\t\tEnter rec number : ";

cin>>rec_no;

}

void display4()

{

cout<<"\n\n\t\tThe rec number is : "<<rec_no;

}

};

Page 23: Pratik Bakane C++

void main()

{

clrscr();

fix_dip f;

f.accept1();

f.display1();

f.accept2();

f.display2();

f.accept3();

f.display3();

f.accept4();

f.display4();

getch();

}

Page 24: Pratik Bakane C++

OUTPUT :

Page 25: Pratik Bakane C++

/*Hierachical Inheriance*/

#include<iostream.h>

#include<conio.h>

class Employee

{

private:

char emp_nm[23];

int emp_id;

public:

void accept1()

{

cout<<"\n\n\t\tEnter the Employee name : ";

cin>>emp_nm;

cout<<"\n\n\t\tEnter the employee ID : ";

cin>>emp_id;

}

void display1()

{

cout<<"\n\n\t\tThe employee Name is : "<<emp_nm;

Employee

Manager Worker

Page 26: Pratik Bakane C++

cout<<"\n\n\t\tThe employee ID is : "<<emp_id;

}

};

class worker:public Employee

{

private:

int sal;

public:

void accept2()

{

cout<<"\n\n\t\tEnter the salary : ";

cin>>sal;

}

void display2()

{

cout<<"\n\n\t\tThe salary is : "<<sal;

}

};

class Manager:public Employee

{

private:

int allowance;

public:

Page 27: Pratik Bakane C++

void accept3()

{

cout<<"\n\n\t\tEnter the total allowance : ";

cin>>allowance;

}

void display3()

{

cout<<"\n\n\t\tThe total allowance is : "<<allowance;

}

};

void main()

{

clrscr();

worker w;

w.accept1();

w.display1();

w.accept2();

w.display2();

Manager m;

m.accept1();

m.display1();

m.accept3();

m.display3();

Page 28: Pratik Bakane C++

getch();

}

OUTPUT :

Page 29: Pratik Bakane C++
Page 30: Pratik Bakane C++

/*Program to declare a class book containing data members

book_title,author_name and price. Accept and display the

information for one object of the class using pointer to that

object.*/

#include<iostream.h>

#include<conio.h>

class book

{

int price;

char title[14],name[20];

public:

void getdata()

{

cout<<"\n\n\t\tEnter the title of the Book : ";

cin>>title;

cout<<"\n\n\t\tEnter the name of the Author :

";

cin>>name;

cout<<"\n\n\t\tEnter the price of the Book : ";

cin>>price;

}

Page 31: Pratik Bakane C++

void putdata()

{

cout<<"\n\n\t\tThe title of the Book is :

"<<title;

cout<<"\n\n\t\tThe name of the Book is :

"<<name;

cout<<"\n\n\t\tThe price of the Book is :

"<<price;

}

};

void main()

{

book b,*p;

clrscr();

p=&b;

p->getdata();

p->putdata();

getch();

}

Page 32: Pratik Bakane C++

OUTPUT

Page 33: Pratik Bakane C++

/*Program to declare a class ‘box’ having data members

height,width and breadth.Accept this information for one

object using pointer to that object. Display the area and

volume of that object.*/

#include<iostream.h>

#include<conio.h>

class box

{

int height,width,breadth;

public:

void getdata()

{

cout<<"\n\n\t\tEnter the height : ";

cin>>height;

cout<<"\n\n\t\tEnter the width : ";

cin>>width;

cout<<"\n\n\t\tEnter the breadth : ";

cin>>breadth;

}

void area()

{

Page 34: Pratik Bakane C++

cout<<"\n\n\t\tArea of the box is :

"<<breadth*width;

}

void volume()

{

cout<<"\n\n\t\tVolume of the Box is :

"<<height*width*breadth;

}

};

void main()

{

clrscr();

box b,*p;

p=&b;

p->getdata();

p->area();

p->volume();

getch();

}

Page 35: Pratik Bakane C++

OUTPUT :

Page 36: Pratik Bakane C++

/*program to declare a class birthday having data members day, month

and year. Accept this information for 4 object using pointer to the

array of object.*/

#include<iostream.h>

#include<conio.h>

class birthday

{

private:

int day,month,year;

public:

void getdata()

{

cout<<"\n\tEnter the Day : ";

cin>>day;

cout<<"\n\tEnter the Month : ";

cin>>month;

cout<<"\n\tEnter the Year : ";

cin>>year;

}

};

void main()

{

Page 37: Pratik Bakane C++

int i;

clrscr();

birthday *b[4];

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

{

b[i]->getdata();

}

getch();

}

Page 38: Pratik Bakane C++

OUTPUT

Page 39: Pratik Bakane C++
Page 40: Pratik Bakane C++
Page 41: Pratik Bakane C++
Page 42: Pratik Bakane C++