Example:2
Write a C++ program to declare a class named person with attributes as name and age. Take input for details of two person check and print the details of the younger person.
Sol:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class person
{
private:
char name[20];
int age;
public:
person(); //default cons
person(char *n,int a); //para cons
void read();
void show();
person younger(person p);
};
//defa cons
person::person()
{
name[0]='\0';
age=0;
}
//para cons
person::person(char *n,int a)
{
strcpy(name,n);
age=a;
}
void person::read()
{
cout<<"Enter name and age ";
cin>>name>>age;
}
void person::show()
{
cout<<"name "<<name<<" age "<<age<<endl;
}
person person::younger(person p)
{
if(age<p.age) //if(this->age<p.age)
return(*this);
else
return(p);
}
/*
*this represents the object using which the member function
is called.
*/
int main()
{
//person p1("mohit",26);
//person p2("sumit",18);
person p1,p2;
p1.read(); p2.read();
person p3;
p3=p1.younger(p2);
p3.show();
getch();
return(0);
}
Output:
Enter name and age manan
25
Enter name and age mohan
14
name mohan age 14
Example:3
Declare a class named student with attributes as roll, name and per. Take input for details of 2 students check and print the details of the student higher in the merit list.
(hint: compare per: details of the student with higher per has to be displayed)
Sol:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class student
{
private:
int roll;
char name[20];
int per;
public:
student(); //default cons
student(int r,char *n,int p); //para cons
void read();
void show();
student higher(student p);
};
//defa cons
student::student()
{
roll=0;
name[0]='\0';
per=0;
}
//para cons
student::student(int r,char *n,int p)
{
roll=r;
strcpy(name,n);
per=p;
}
void student::read()
{
cout<<"Enter roll, name and per ";
cin>>roll>>name>>per;
}
void student::show()
{
cout<<"roll "<<roll<<" name "<<name<<" per "<<per<<endl;
}
student student::higher(student p)
{
if(per>p.per) //if(this->per>p.per)
return(*this);
else
return(p);
}
/*
*this represents the object using which the member function
is called.
*/
int main()
{
//student p1("mohit",26);
//student p2("sumit",18);
student p1,p2;
p1.read(); p2.read();
student p3;
p3=p1.higher(p2);
p3.show();
getch();
return(0);
}
Output:
Enter roll, name and per 101 aman 98
Enter roll, name and per 102 anmool 85
roll 101 name aman per 98
Example:4
Declare a class named employee with attributes as empno, name and salary ,take input for details of two employees , display the details of the employee getting higher salary.
Sol:
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class employee
{
private:
int empno;
char name[20];
int sal;
public:
employee(); //default cons
employee(int e,char *n,int s); //para cons
void read();
void show();
employee higher(employee p);
};
//defa cons
employee::employee()
{
empno=0;
name[0]='\0';
sal=0;
}
//para cons
employee::employee(int e,char *n,int s)
{
empno=e;
strcpy(name,n);
sal=s;
}
void employee::read()
{
cout<<"Enter empno, name and sal ";
cin>>empno>>name>>sal;
}
void employee::show()
{
cout<<"empno "<<empno<<" name "<<name<<" sal "<<sal<<endl;
}
employee employee::higher(employee p)
{
if(sal>p.sal) //if(this->sal>p.sal)
return(*this);
else
return(p);
}
/*
*this represents the object using which the member function
is called.
*/
int main()
{
//employee p1("mohit",26);
//employee p2("sumit",18);
employee p1,p2;
p1.read(); p2.read();
employee p3;
p3=p1.higher(p2);
p3.show();
getch();
return(0);
}
Output:
Enter empno, name and sal 101
gopal
45000
Enter empno, name and sal 102
mohan
65000
empno 102 name mohan sal 65000




