prowessapps.in Structure, Class and Objects Programs in C++

#include< iostream>
using namespace std;
struct Book {
 string author;
 string title;
 int price;
};
int main() {
 Book b[5];
 for(int i = 0;i<5; i++) {
  cout<<"Enter Author ";
  getline(cin,b[i].author);
  cout<<"Enter Title ";
  getline(cin,b[i].title);
  cout<<"Enter Price ";
  cin>>b[i].price;
  cin.ignore(1,'\n');
 }
 for(int i=0; i<5; i++) {
  cout<< b[i].author<<"\t"<< b[i].title;
  cout<<"\t"<< b[i].price<< endl;
 }
 return 0;
}
OUTPUT
 Enter Author Faiz
 Enter Title Java
 Enter Price 500
 Enter Author Alok
 Enter Title C
 Enter Price 400
 Enter Author Dan
 Enter Title C++
 Enter Price 600
 Enter Author Ayan
 Enter Title Android
 Enter Price 1000
 Enter Author Bala
 Enter Title J2SE
 Enter Price 700
Faiz	Java	500
Alok	C	400
Dan	C++	600
Ayan	Android	1000
Bala	J2SE	700

#include< iostream>
using namespace std;
struct Emp {
 string name;
 int id;
 int bsal;
 struct DOB {
  int d;
  int m;
  int y;
 }dob;
};
int main() {
 Emp e[5];
 for(int i = 0;i<5;i++) {
  cout<<"Enter Name ";
  getline(cin,e[i].name);
  cout<<"Enter id";
  cin>>e[i].id;
  cout<<"Enter basic salary ";
  cin>>e[i].bsal;
  cout<<"Enter Date(day)";
  cin>>e[i].dob.d;
  cout<<"Enter Month ";
  cin>>e[i].dob.m;
  cout<<"Enter Year ";
  cin>>e[i].dob.y;
  cin.ignore(1,'\n');
 }
 int max = e[0].bsal;
 Emp t = e[0];
 for(int i = 0;i<5;i++) {
  if(e[i].bsal>max){
   max = e[i].bsal;
   t = e[i];
  }
 }
 cout<<"Emp. having highest salary \n";
 cout<< t.name<<"\t"<< t.id<<"\t";
 cout<< t.bsal<<"\t"<< t.dob.d<<"-";
 cout<< t.dob.m<<"-"<< t.dob.y<< endl;
cout<<"\n\nSorted Detail \n";
for(int i = 1; i<=5; i++) {
 for(int j = 0; j<5-i;i++) {
  if(e[j].bsal>e[j+1].bsal) {
   t = e[j];
   e[j] = e[j+1];
   e[j+1] = t;
  }
 }
}
for(int i = 0; i<5; i++) {
 cout<< e[i].name<<"\t"<< e[i].id;
 cout<<"\t"<< e[i].bsal<<"\t";
 cout<< e[i].dob.d<<"-";
 cout<< e[i].dob.m<<"-";
 cout<< e[i].dob.y<< endl;
 }
 return 0;
}
OUTPUT
Enter Name Anup
Enter id 12
Enter basic salary 22000
Enter Date(day) 11
Enter Month 10
Enter year 1994
Enter Name Ashok
Enter id 15
Enter basic salary 18000
Enter Date(day) 12
Enter Month 11
Enter year 1990
Enter Name Ayan
Enter id 51
Enter basic salary 50000
Enter Date(day) 17
Enter Month 12
Enter year 2014
Enter Name XYZ
Enter id 11
Enter basic salary 8000
Enter Date(day) 3
Enter Month 3
Enter year 1993
Enter Name Vinay
Enter id 23
Enter basic salary 15000
Enter Date(day) 12
Enter Month 12
Enter year 1990
Employee having highest salary
Ayan	51	50000		17-12-2014

Sorted Detail
XYZ	11	8000		3-3-1993
Vinay	23	15000		12-12-1990
Ashok	15	18000		12-11-1990
Anup	12	22000		11-10-1994
Ayan	51	50000		17-12-2014

#include< iostream>
using namespace std;
class Emp {
 string name;
 int id,bsal;
 public :
 void input() {
  cout<<"Enter Name ";
  getline(cin,name);
  cout<<"Enter Id ";
  cin>>id;
  cout<<"Enter Basic Salary ";
  cin>>bsal;
  cin.ignore(1,'\n');
  }
 void display() {
  cout<< id<<"\t"<< name<<"\t";
  cout<< bsal<<"\t"<< 12*bsal<< endl;
 }
};
int main() {
 Emp e[5];
 for(int i = 0; i<5;i++) {
  e[i].input();
 }
 for(int i = 0; i<5;i++ ) {
  e[i].display();
 }
return 0;
}
OUTPUT
Enter Name Anup
Enter id 12
Enter basic salary 22000
Enter Name Ashok
Enter id 15
Enter basic salary 18000
Enter Name Ayan
Enter id 51
Enter basic salary 50000
Enter Name XYZ
Enter id 11
Enter basic salary 8000
Enter Name Vinay
Enter id 23
Enter basic salary 15000
Anup	12	22000		264000
Ashok	15	18000		216000
Ayan	51	50000		600000
XYZ	11	8000		96000
Vinay	23	15000		180000

#include< iostream>
using namespace std;
class Complex {
 public :
 int r,i;
 void add(Complex c1, Complex c2 ) {
  int x = c1.r+c2.r;
  int y = c1.i+c2.i;
  cout<<"Sum = "<< x<<" +i"<< y;
  }
};
int main() {
 Complex c1,c2;
 cout<<"Enter Real & Imag. of Num1 :";
 cin>>c1.r>>c1.i;
 cout<<"Enter Real & Imag. of Num2 :";
 cin>>c2.r>>c2.i;
 c1.add(c1,c2);
 return 0;
}
OUTPUT
Enter the Real and Imag. of Num1 :5 10
Enter the Real and Imag. of Num2 :6 20
Sum = 11 +i30


Get it on Google Play


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved