Pointer To Object
Pointer To Object
#include<iostream.h>
#include<conio.h>
class item
{
int code; float price;
public: void get()
{
cout<<"\nEnter Code and Price : ";
cin>>code;
cin>>price;
}
void show()
{
cout<<"\nCode : "<<code<<"\nPrice : "<<price;
}
};
void main()
{
clrscr();
item *p=new item[5];
item *q;
int i;
q=p;
for(i=0;i<5;i++)
{
p->get();
p++;
}
for(i=0;i<5;i++)
{
cout<<"\nObject : "<<i+1;
q->show();
q++;
}
getch();
}
OUTPUT
Pointer to derived class
#include<iostream.h>
#include<conio.h>
class B
{
public: int b;
void show()
{
cout<<"\nB="<<b;
}
};
class D:public B
{
public: int d;
void show()
{
cout<<"\nD="<<d;
cout<<"\nB="<<b;
}
};
void main()
{
clrscr();
B *bptr,base;
D *dptr,derived;
bptr=&base;
bptr->b=100;
bptr->show();
bptr=&derived;
bptr->b=200;
bptr->show();
dptr=&derived;
dptr->d=300;
dptr->show();
getch();
}
Output
Area of shapes Using pointers to derived class
#include<iostream.h>
#include<conio.h>
class shape
{
protected: double dim1,dim2;
public: virtual void disparea()
{ cout<<" "; }
void getdata()
{
cout<<"\nEnter 2 dimensions : ";
cin>>dim1>>dim2;
}
};
class triangle:public shape
{
public: void disparea()
{
double area=0.5*dim1*dim2;
cout<<"Area : "<<area;
}
};
class rectangle:public shape
{
public: void disparea()
{
double area=dim1*dim2;
cout<<"Area : "<<area;
}
};
void main()
{
shape s; triangle t; rectangle r;
clrscr();
shape *ptr;
ptr=&t;
cout<<"\n\nTriangle ";
ptr->getdata();
ptr->disparea();
ptr=&r;
cout<<"\n\nRectangle ";
ptr->getdata();
ptr->disparea();
getch();
}
OUTPUT
THIS Pointer
#include<iostream.h>
#include<conio.h>
class person
{
char name[30]; float age;
public: void get()
{
cout<<"Enter Name and Age : ";
cin>>name;
cin>>age;
}
person &person::greater(person &x)
{
if(x.age>=age)
return x;
else
return *this;
}
void disp()
{
cout<<"\nName : "<<name<<"\nAge : "<<age;
}
};
void main()
{
clrscr();
person p1,p2;
p1.get();
p2.get();
person p=p1.greater(p2);
p.disp();
getch();
}
OUTPUT
CLASS TEMPLATES
#include<iostream.h>
#include<conio.h>
template<class T>
class test
{
T a,b;
public: test(T x,T y)
{
a=x;
b=y;
}
void show()
{
cout<<"\nA="<<a<<"\nB="<<b;
}
}
void main()
{
clrscr();
test<int>T1(2,3);
test<float>T2(1.1141,3.1415);
cout<<"\n\nT1 ";
T1.show();
cout<<"\n\n\nT2 ";
T2.show();
getch();
}
OUTPUT
Minimum of an array using templates
#include<iostream.h>
#include<conio.h>
for(i=0;i<n;i++)
{
if(a[i]<min)
min=a[i];
}
cout<<"\nMinimum Value : "<<min;
}
void main()
{
int a[50],n,i;
float b[50];
clrscr();
cout<<"\nEnter number of elements in the arrays : ";
cin>>n;
void main()
{
clrscr();
int i,j;
char s[5][20];
for(i=0;i<5;i++)
cin>>s[i];
clrscr();
for(i=0;i<5;i++)
{
for(j=1;j<5;j++)
{
if(int(s[j-1][0])>int(s[j][0]))
{
char temp[20];
strcpy(temp,s[j-1]);
strcpy(s[j-1],s[j]);
strcpy(s[j],temp);
}
}
}
getch();
}
OUTPUT
Unformatted and Formatted I/O example
#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
char name[30],str1[3]="C++";
char str2[11]="programming";
int m=3,n=11;
for(int i=0;i<n;i++)
{
cout.write(str2,i);
cout<<"\n";
}
for(i=n;i>0;i--)
{
cout.write(str2,i);
cout<<"\n";
}
cout.write(str1,m).write(str2,n);
cout<<"\n";
getch();
clrscr();
cout<<"Getline+write - \n";
cin.getline(name,30);
cout.write(name,30);
cout<<"\n";
cout<<"Width - ";
cout.width(5);
cout<<543<<12<<"\n\n";
cout.width(5);
cout<<543;
cout<<"\n\n";
cout.width(5);
cout<<12;
cout<<"\n\n";
cout<<"Precision3 - ";
cout.precision(3);
cout<<sqrt(2)<<" "<<3.1415926<<" "<<2.50032<<"\n\n";
cout<<"Fill and various ios flags -\n ";
cout.fill('*');
cout.width(10);
cout<<5250<<"\n\n";
cout.fill('*');
cout.setf(ios::left,ios::adjustfield);
cout.width(15);
cout<<"Table 1"<<endl<<endl;
cout.fill('*');
cout.precision(3);
cout.setf(ios::internal,ios::adjustfield);
cout.setf(ios::scientific,ios::floatfield);
cout.width(15);
cout<<-12.34567<<endl<<endl;
cout.setf(ios::showpoint);
cout<<2.5<<endl<<endl;
cout.unsetf(ios::showpoint);
cout.setf(ios::showpos);
cout<<35<<endl<<8.192<<endl;
getch();
}
OUTPUT