Read OnLine Cpp Program Example



9 - CLASS
PRAC 5 PRAC 6

// classes example
#include<iostream.h>
#include<conio.h>
class demo
{
public:
void add (int a,int b)
{
int ans;
ans=a+b;
cout<<"Add="<<ans;
}
void add (int a,int b,int c)
{
int ans;
ans=a+b+c;
cout<<"\nAdd="<<ans;
}
};
void main()
{
clrscr();
demo d1;
d1.add(4,5);
d1.add(4,5,6);
getch();
}
---- OUTPUT -----
Add= 9
Add= 15

// classes example
#include<iostream.h>
#include<conio.h>
class cvector
{
public:
int x,y;
cvector(){};
cvector(int,int);
cvector operator + (cvector);
};
cvector::cvector (int a,int b)
{
x=a;
y=b;
}
cvector cvector::operator+ (cvector param)
{
cvector temp;
temp.x=x+param.x;
temp.y=y+param.y;
return (temp);
}
void main()
{
clrscr();
cvector a(3,1);
cvector b(1,2);
cvector c;
c=a+b;
cout<<c.x<<","<<c.y;
getch();
}
---- OUTPUT -----
4,3

Previous Next
Download Download