Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

Friday, 29 April 2016

Friend Function Example.

#include<iostream.h>
#include<conio.h>
class square
{
int no;
public:
void accept()
{
cout<<"Enter no for square:";
cin>>no;
}
friend void sq(square s);
};
void sq(square s)
{
int ans;
ans=s.no*s.no;
cout<<"Square="<<ans;
}
void main()
{
clrscr();
square s;
s.accept();
sq(s);
getch();
}
/*Output:
Enter no for square:4
Square=16 */

WAP for finding out the weight on any planet using static data member

#include<iostream.h>
#include<conio.h>
class planets
{
static float g;
float w,m;
int ch;
public:
void accept()
{
cout<<"Find weight on:"<<endl;
cout<<"1:MERCURY\n2:VENUS\n3:EARTH\n4:MARS\n5:JUPITER\n6:SATURN\n7:URANUS\n8:NEPTUNE"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
g=3.61;
break;
case 2:
g=8.83;
break;
case 3:
g=9.8;
break;
case 4:
g=3.75;
break;
case 5:
g=26.0;
break;
case 6:
g=11.2;
break;
case 7:
g=10.5;
break;
case 8:
g=13.3;
break;
}
cout<<"Enter mass:";
cin>>m;
}
void display()
{
w=m*g;
cout<<"Weight="<<w;
}
};
float planets::g;
void main()
{
clrscr();
planets p;
p.accept();
p.display();
getch();
}
/*Output:
Find weight on:
1:MERCURY                                                                      
2:VENUS                                                                        
3:EARTH                                                                        
4:MARS                                                                        
5:JUPITER                                                                      
6:SATURN                                                                      
7:URANUS                                                                      
8:NEPTUNE                                                                      
Enter your choice:                                                            
3
Enter mass:10
Weight=98  */

Class with Static Data Member

#include<iostream.h>
#include<conio.h>
class account
{
static int r;
int p,n,si;
public:
void accept()
{
cout<<"Enter Principal amount & No. of years:"<<endl;
cin>>p>>n;
}
void display()
{
si=(p*n*r)/100;
cout<<"Simple Interest="<<si;
}
};
int account::r=10;
void main()
{
clrscr();
account a;
a.accept();
a.display();
getch();
}
/*Output:
Enter Principal amount & No. of years:
500 2                                                                          
Simple Interest=100 */

Thursday, 28 April 2016

Write a program showing Friend Function

 #include<iostream.h>
 #include<conio.h>
 class b;
 class a
 {
int no1;
public:
void accept()
{
cout<<"Enter no 1:";
cin>>no1;
}
void display()
{
cout<<"No 1 is greater";
}
friend void great(a,b);
 };
 class b
 {
int no2;
public:
void accept()
{
cout<<"Enter no 2:";
cin>>no2;
}
void display()
{
cout<<"No 2 is greater";
}
friend void great(a,b);
 };
 void great(a m,b n)
 {
if(m.no1>n.no2)
{
m.display();
}
else
{
n.display();
}
 }
 void main()
 {
clrscr();
a m;
b n;
m.accept();
n.accept();
great(m,n);
getch();
}
/*Output:
Enter no 1:23
Enter no 2:21                                                                  
No 1 is greater */

Array Of Objects With Conditions

#include<iostream.h>
#include<conio.h>
class city
{
int pop;
char name[20];
public:
void accept()
{
cout<<"enter name and population"<<endl;
cin>>name>>pop;
}
void display();
}c[5];
void city::display()
{
int max=0,i;
for(i=0;i<5;i++)
{
if(max<c[i].pop)
max=c[i].pop;
}
for(i=0;i<5;i++)
{
if(max==c[i].pop)
{
cout<<"Max Populated city="<<c[i].name<<endl;
cout<<"Population="<<max;
}
}
}
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
{
c[i].accept();
}
c[i].display();
getch();
}
/*Output:
enter name and population
qqq 123                                                                      
enter name and population
ddd 456
enter name and population
ttt 677
enter name and population
uuu 876
enter name and population
ttg 667
Max Populated city=uuu
Population=876 */

Wednesday, 27 April 2016

What is Array Of Objects

Like, Array is a collection of variables of same data type, Similarly the Array Of Objects are the collection of objects of same class type.

Arrays of variables of type "class" is known as "Array of objects". The "identifier" used to refer the array of objects is an user defined data type.

Example:



#include <iostream.h>
const int MAX =100;
class Details
{
private:
int salary;
float roll;
public:
void getname( )
{
cout << "\n Enter the Salary:";
cin >> salary;
cout << "\n Enter the roll:";
cin >> roll;
}
void putname( )
{
cout << "Employees" << salary <<
"and roll is" << roll << '\n';
}
};
void main()
{
Details det[MAX];
int n=0;
char ans;
do{
cout << "Enter the Employee Number::" << n+1;
det[n++].getname;
cout << "Enter another (y/n)?: " ;
cin >> ans;
} while ( ans != 'n' );
for (int j=0; j<n; j++)
{
cout << "\nEmployee Number is:: " << j+1;
det[j].putname( );
}
}

Result:



Enter the Employee Number:: 1
Enter the Salary:20
Enter the roll:30
Enter another (y/n)?: y
Enter the Employee Number:: 2
Enter the Salary:20
Enter the roll:30
Enter another (y/n)?: n

Array Of Objects With Classes

#include<iostream.h>
#include<conio.h>

class account
{
int accno;

public:
int balance;

void accept()
{
cout<<endl<<"Enter account no:";
cin>>accno;
cout<<"Enter balance:";
cin>>balance;

}

void display()
{
cout<<"-Balance after given interest of 10%:- "<<endl;
cout<<"Account no:"<<accno;
cout<<endl<<"Balance:"<<balance<<endl;
}
}a[10];

void main()
{
int i,balance;
clrscr();
for(i=0;i<10;i++)
{
a[i].accept();
if(a[i].balance>=5000)
{
a[i].balance=a[i].balance+(a[i].balance*0.1);
a[i].display();
}

}
getch();
}
/*Output:
Enter account no:45
Enter balance:5000
-Balance after given interest of 10%:-
Account no:45
Balance:5500

Enter account no:22
Enter balance:2000

Enter account no:48
Enter balance:200

Enter account no:33
Enter balance:100

Enter account no:66
Enter balance:777

Enter account no:88
Enter balance:100

Enter account no:19
Enter balance:9000
-Balance after given interest of 10%:-
Account no:19
Balance:9900

Enter account no:30
Enter balance:200

Enter account no:99
Enter balance:7777
-Balance after given interest of 10%:-
Account no:99
Balance:8554

Enter account no:77
Enter balance:1200

*/

Class and Objects Program

#include<iostream.h>
#include<conio.h>
class day        //class
{
int hrs,min,sec;
public:
void accept()
{
cout<<"Enter min:";
cin>>min;
}
void display()
{
if(min>60)
{
hrs=min/60;
min=min%60;
cout<<hrs<<":"<<min;
}
else
{
sec=min*60;
cout<<sec;
}
}
};
void main()
{
clrscr();
day d;     //object
d.accept();
d.display();
getch();
}
/*Output:
Enter min:138
2:18 */

Simple Example For Class

#include<iostream.h>
#include<conio.h>
class calender
{
private:
int day,month,year;
public:
void accept()
{
cout<<"Enter the day,month,year:";
cin>>day>>month>>year;
}
void display()
{
cout<<day<<"/"<<month<<"/"<<year;
}
};
void main()
{
calender c;
clrscr();
c.accept();
c.display();
getch();
}

/*OUTPUT:
Enter the day,month,year:2 4 2016
2/4/2016*/