0% found this document useful (0 votes)
52 views

Pointer To Object

The document discusses various C++ input/output concepts including formatted and unformatted I/O, getline, write, width, precision, fill flags, and adjustment flags. Examples are provided to demonstrate these concepts such as reading and writing strings, integers, and floating point numbers with different field widths, precisions, and formats. The document also shows examples of left, right, and internal adjustment as well as positive, scientific, and default floating point formats.

Uploaded by

prey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Pointer To Object

The document discusses various C++ input/output concepts including formatted and unformatted I/O, getline, write, width, precision, fill flags, and adjustment flags. Examples are provided to demonstrate these concepts such as reading and writing strings, integers, and floating point numbers with different field widths, precisions, and formats. The document also shows examples of left, right, and internal adjustment as well as positive, scientific, and default floating point formats.

Uploaded by

prey
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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>

template<class T,class T1>


void min(T a[50],T1 n)
{
int i;
T min;
min=a[0];

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;

cout<<"\nEnter Integer elements : \n";


for(i=0;i<n;i++)
cin>>a[i];

cout<<"\nEnter Float elements : \n";


for(i=0;i<n;i++)
cin>>b[i];

cout<<"\n\n\nInteger elements : ";


for(i=0;i<n;i++)
cout<<"\t"<<a[i];
min(a,n);

cout<<"\n\n\nFloat elements : ";


for(i=0;i<n;i++)
cout<<"\t"<<b[i];
min(b,n);
getch();
}
OUTPUT
Sorting string Alphabetically
#include<iostream.h>
#include<conio.h>
#include<string.h>

void main()
{
clrscr();
int i,j;
char s[5][20];

for(i=0;i<5;i++)
cin>>s[i];
clrscr();

cout<<"Before Sorting : ";


for(i=0;i<5;i++)
cout<<" \n String "<<i+1<<" - "<<s[i];

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);
}

}
}

cout<<"\n\nAfter Sorting : ";


for(i=0;i<5;i++)
cout<<" \n String "<<i+1<<" - "<<s[i];

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

You might also like