Professional Documents
Culture Documents
: - Member operator
::* - Pointer to Member declarator
==>* - Pointer to Member Operator
* - Pointer to Member Operator
new - memeory allocation
delete - release Operator
Manipultors
**********
manipulator functions are speical stream functions
that change certain characters of the input and output.
the main advandage of using functions is that they facilite
the formatting of input and output streams.
endl:
endl is an used to line feed character or new line.
1. setbase()
2. Setw()
3. setfill()
4. setprecision()*/
// Setbase()
/*#include<iostream.h>
#include<conio.h>
#include<iomanip.h>*/
/*void main()
{
int a=12;
clrscr();
cout<<"Decimal value of 12 is:"<<setbase(12)<<dec<<a<<endl;
cout<<"Octal value of 12 is:"<<setbase(12)<<oct<<a<<endl;
cout<<"Hexa decimal of 12 is:"<<setbase(12)<<hex<<a<<endl;
getch();
}*/
/* void main()
{
int a=10,b=20;
clrscr();
cout<<setw(1)<<a<<setw(15)<<b<<endl;
cout<<setw(5)<<a<<setw(20)<<b<<endl;
getch();
}*/
//3.setfill()
/*void main()
{
int a=10,b=20;
clrscr();
cout<<setfill('*');
cout<<setw(5)<<a<<setw(15)<<b<<endl;
cout<<setfill('#');
cout<<setw(7)<<a<<setw(17)<<b<<endl;
getch();
}*/
// 4.setprecison
/*void main()
{
int a=9,b=3;
float c=(float)a/b;
clrscr();
cout<<setprecision(1)<<c<<endl;
cout<<setprecision(2)<<c<<endl;
cout<<setprecision(3)<<c<<endl;
cout<<setprecision(4)<<c<<endl;
getch();
}*/
/* void main()
{
int a=12,b=4;
float f;
clrscr();
c=a/b;
cout<<"\n"<<c;
getch();
}*/
Access specifiers
*****************
private:
Access only within class
public:
Access only any where in the class
protected:
Access within the class and any class immediately dervied from it.
syntax
*******
class class_name
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
};
*/
/*#include<iostream.h>
#include<conio.h>
class item
{
int no;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<"Number:"<<no<<"\n";
cout<<"Cost:"<<cost<<"\n";
}
};
/*Member functions
*****************
} */
/*#include<iostream.h>
#include<conio.h>
class set
{
int m,n;
public:
void input(void);
void display(void);
int largest(void);
};
int set::largest(void)
{
if(m>=n)
return(m);
else
return(n);
}
void set::input(void)
{
cout<<"\n Input values of m and n"<<"\n";
cin>>m>>n;
}
void set::display(void)
{
cout<<"largest value=";
cout<<largest()<<"\n";
}
int main()
{
clrscr();
set A;
A.input();
A.display();
return 0;
}*/
/*#include<iostream.h>
#include<conio.h>
const m=50;
class items
{
int icode[m];
float iprice[m];
int count;
public:
void CNT(void)
{
count=0;
}
void getitem(void);
void displaysum(void);
void remove(void);
void displayitem(void);
};
void items::getitem(void)
{
cout<<"Enter item code:";
cin>>icode[count];
cout<<"Enter item Cost:";
cin>>iprice[count];
count++;
}
void items::displaysum(void)
{
float sum=0;
for(int i=0;i<count;i++)
sum=sum+iprice[i];
cout<<"\n Total value:"<<sum<<"\n";
}
void items::remove(void)
{
int a;
cout<<"Enter item code:";
cin>>a;
for(int i=0;i<count;i++)
if(icode[i]==a)
iprice[i]=0;
}
void items::displayitem(void)
{
cout<<"\n Code \t\t price\n";
for(int i=0;i<count;i++)
{
cout<<"\n"<<icode[i];
cout<<"\t\t"<<iprice[i];
}
cout<<"\n";
}
int main()
{
items order;
order.CNT();
int x;
clrscr();
do
{
cout<<"\n you can do the following:";
cout<<"\n Enter approximate Number\n";
cout<<"\n \n 1.Add an Item";
cout<<"\n \n 2.Display total value";
cout<<"\n \n 3.Delete an Item";
cout<<"\n \n 4.Display all items:";
cout<<"\n \n 5.Quit";
cout<<"\n\n What is your options:";
cin>>x;
switch(x)
{
case 1:
order.getitem();
break;
case 2:
order.displaysum();
break;
case 3:
order.remove();
break;
case 4:
order.displayitem();
break;
default:
cout<<"\n Error in input try again\n\n";
}
}
while(x!=5);
return 0;
} */
/*#include<iostream.h>
#include<conio.h>
class emp
{
char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
void emp::getdata(void)
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter Age:";
cin>>age;
}
void emp::putdata(void)
{
cout<<"\n name:"<<name<<"\n";
cout<<"\t\t Age:"<<age<<"\n";
}
const int size=3;
int main()
{
emp manager[size];
clrscr();
for(int i=0;i<size;i++)
{
cout<<"\n Details of Manager"<<i+1<<"\n";
manager[i].getdata();
}
cout<<"\n";
for(i=0;i<size;i++)
{
cout<<"\n Manager"<<i+1<<"\n";
manager[i].putdata();
}
return 0;
}*/
/* *********** */
/*
FRIEND fUNCTIONS
****************
syntax
******
class class_name
{
--------
-------
public:
------
-------
friend void var(void);
};
*/
/*#include<iostream.h>
#include<conio.h>
class sample
{
int a,b;
public:
void setvalue()
{
a=25;
b=40;
}
friend float mean(sample s);
};
float mean(sample s)
{
return float(s.a+s.b)/2.0;
}
int main()
{
sample x;
clrscr();
x.setvalue();
cout<<"mean value="<<mean(x)<<"\n";
return 0;
}*/
/*#include<iostream.h>
#include<conio.h>
class abc;
class xyz
{
int x;
public:
void setvalue(int i)
{
x=i;
}
class abc
{
int a;
public:
void setvalue(int i)
{
a=i;
}
friend void max(xyz,abc);
};
int main()
{
abc aa;
aa.setvalue(10);
xyz xx;
xx.setvalue(20);
max(xyz,abc);
return 0;
} */
/*
CONSTRUCTORS
*************
syntax
*****
class class_name
{
------
-------
public:
const_name(void);
---------
----------
};
class_name::const_name(void)
{
-----
------
}*/
/*#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
public:
integer(int,int);
void display(void)
{
cout<<"m="<<m<<"\n";
cout<<"n="<<n<<"\n";
}
};
integer::integer(int x,int y)
{
m=x;
n=y;
}
int main()
{
clrscr();
integer int1(0,100);
integer int2=integer(25,75);
cout<<"\n Object1"<<"\n";
int1.display();
cout<<"\n object2"<<"\n";
int2.display();
return 0;
}*/
//Overloaded constructors
/*#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{ //first const
}
complex(float a)
{
x=y;
y=a; //second cons
}
void show(complex c)
{
cout<<c.x<<"+i"<<c.y<<"\n";
}
int main()
{
clrscr();
complex A(2.7,3.5);
complex B(1.6,3.5);
complex C;
C=sum(A,B);
cout<<"A=";
show(A);
cout<<"B=";
show(B);
cout<<"C=";
show(C);
complex P,Q,R;
P=complex(2.5,3.9);
Q=complex(1.6,2.5);
R=sum(P,Q);
cout<<"\n";
cout<<"P=";
show(P);
cout<<"Q=";
show(Q);
cout<<"R=";
show(R);
return 0;
}*/
//COPY CONSTRUCTOR
/*
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code()
{ //first const
}
code(int a)
{
id=a;
} //second cons
code(code & x)
{
id=x.id;
}
void display(void)
{
cout<<id;
}
};
int main()
{
clrscr();
code A(100);
code B(A);
code C=A;
code D;
D=A;
cout<<"\n id of A:";
A.display();
cout<<"\n Id of B:";
B.display();
cout<<"\n Id of C:";
C.display();
cout<<"\n ID of D:";
D.display();
return 0;
}
*/
/*#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *name;
int length;
public:
string()
{
length=0;
name=new char[length+1];
}
string(char *s)
{
length=strlen(s);
name=new char[length+1];
strcpy(name,s);
}
void display(void)
{
cout<<name<<"\n";
}
int main()
{
clrscr();
char *first="raja";
string name1(first),name2("ram"),name3("mohanrai"),s1,s2;
s1.join(name1,name2);
s2.join(s1,name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
return 0;
}
/* Inheritance */
//Single Inheritance
/* #include<iostream.h>
#include<conio.h>
class B
{
int a;
public:
int b;
void get_ab();
int get_a(void);
void show_a(void);
};
class D:public B
{
int c;
public:
void mul(void);
void display(void);
};
void B::get_ab(void)
{
a=5;
b=10;
}
int B::get_a()
{
return a;
}
void B::show_a()
{
cout<<"A="<<a<<"\n";
}
void D::mul()
{
c=b*get_a();
}
void D::display()
{
cout<<"A="<<get_a()<<"\n";
cout<<"B="<<b<<"\n";
cout<<"C="<<c<<"\n\n";
}
int main()
{
clrscr();
D d;
d.get_ab();
d.mul();
d.show_a();
d.display();
d.b=20;
d.mul();
d.display();
return 0;
}*/
//MultiLevel Inheritance
/*#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void get_no(int);
void put_no(void);
};
void student::get_no(int a)
{
rno=a;
}
void student::put_no()
{
cout<<"\n Roll Number:"<<rno<<"\n";
}
void test::put_marks()
{
cout<<"\n Marks in sub1="<<sub1<<"\n";
cout<<"\n Marks in sub2="<<sub2<<"\n";
}
void result::display(void)
{
total=sub1+sub2;
put_no();
put_marks();
cout<<"\n Total="<<total<<"\n\n";
}
int main()
{
clrscr();
result student1;
student1.get_no(101);
student1.get_marks(75,78);
student1.display();
return 0;
}*/
/* DESTRUCTORS
A destructor the name implies is used to destory the objects
that have been created by a constructor.The destructor is a
member function whose name is the same as the class name.*/
/*#include<iostream.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n No of Objetcs created"<<count;
}
~alpha()
{
cout<<"\n No of objects destroyed"<<count;
count--;
}
};
int main()
{
cout<<"\n \n Enter Main\n";
alpha a1,a2,a3,a4;
{
cout<<"\n\n Enter Block\n";
alpha a5;
}
{
cout<<"\n \n Enter block1\n";
alpha a6;
}
{
cout<<"\n\n Enter Block2\n";
alpha a6;
}
cout<<"\n\n re-Enter main\n";
return 0;
}
*/
/* Opeartor Overloading
example
******
return type classname::operator
{
function body
} */
/* #include<iostream.h>
#include<conio.h>
class space
{
int x;
int y;
int z;
public:
void getdata(int a,int b,int c);
void display(void);
void operator-();
};
void space::display(void)
{
cout<<x<<" ";
cout<<y<<" ";
cout<<z<<"\n";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
int main()
{
clrscr();
space S;
S.getdata(10,-20,30);
cout<<"\n S:";
S.display();
-S;
cout<<"S:";
S.display();
-S;
cout<<"S:";
S.display();
return 0;
}*/
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+j"<<y<<"\n";
}
int main()
{
clrscr();
complex c1,c2,c3;
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"c3=";
c3.display();
return 0;
}*/
//Mulitple Inheritance
/*#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
void M::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}
void P::display(void)
{
cout<<"\n M="<<m<<"\n";
cout<<"\n n="<<n<<"\n";
cout<<"\n M*n="<<m*n<<"\n";
}
int main()
{
clrscr();
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
} */
//HYbrid Inheritance
/*#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
cout<<"\n Roll No:"<<rno<<"\n";
}
};
class sports
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"\n sports Wt:"<<score<<"\n\n";
}
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
put_score();
cout<<"\n Total score:"<<total<<"\n";
}
int main()
{
clrscr();
result st1;
st1.get_no(123);
st1.get_marks(86,78);
st1.get_score(8);
st1.display();
return 0;
} */
/* #include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
cout<<"Roll NO:"<<rno<<"\n";
}
};
void put_marks(void)
{
cout<<"Marks Obtained:"<<"\n";
cout<<"Part1="<<part1<<"\n";
cout<<"part2="<<part2<<"\n";
}
};
class sports:public virtual student
{
protected:
float score:
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"sports wt:"<<score<<"\n\n";
}
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
put_score();
cout<< "total score: " <<total << "\n";
}
int main()
{
result student_1;
student_1.get_no(678);
student_1.get_marks(30.5, 25.5);
student_1.get_score(7.0);
student_1.display();
return 0;
}
*/
//POLYMORPHISM
//Pointer to Objects
/* #include<iostream.h>
#include<conio.h>
class item
{
int code;
float price;
public:
void getdata(int a,float b)
{
code=a;
price=b;
}
void show(void)
{
cout<<"Code:"<<code<<"\n";
cout<<"Price:"<<price<<"\n";
}
};
#include<iostream.h>
#include<conio.h>
#include<string.h>
class city
{
protected:
char *name;
int len;
public:
city()
{
len=0;
name=new char[len+1];
}
void getname(void)
{
char *s;
s=new char[30];
cout<<"Enter city Name:";
cin>>s;
len=strlen(s);
name=new char[len+1];
strcpy(name,s);
}
void printname(void)
{
cout<<name<<"\n";
}
};
int main()
{
city *cptr[10];
int n=1;
int option;
clrscr();
do
{
cptr[n]=new city;
cptr[n]->getname();
n++;
cout<<"Do you want to enter one more name?\n";
cout<<"(Enter 1 for yes 0 for no):";
cin>>option;
}
while(option);
cout<<"\n\n";
for(int i=1;i<=n;i++)
{
cptr[i]->printname();
}
return 0;
}*/
//This Pointer
/* #include<iostream.h>
#include<conio.h>
#include<string.h>
class person
{
char name[30];
float age;
public:
person(char *s,float a)
{
strcpy(name,s);
age=a;
}
person & person::greater(person & x)
{
if(x.age>=age)
return x;
else
return *this;
}
void display(void)
{
cout<<"Name:"<<name<<"\n";
cout<<"Age:"<<age<<"\n";
}
};
int main()
{
clrscr();
person p1("Rahim",26.00),p2("peter",25.00),p3("Nagraj",35.00);
person p=p1.greater(p3);
cout<<"\n Enter person is:\n";
p.display();
p2.display();
p3.display();
p=p1.greater(p2);
cout<<"Elder person is:\n";
p.display();
return 0;
}
*/
//Virtual Functions
/*
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n Display Base:";
}
};
void show()
{
cout<<"\n Show derived";
}
};
int main()
{
clrscr();
base b;
derived d;
base *bptr;
bptr->display();
bptr->show();
cout<<"\n\n bptr points to derived\n";
bptr=&d;
bptr->display();
bptr->show();
return 0;
}
*/
// Runtime Polymorphism
/* #include<iostream.h>
#include<conio.h>
#include<string.h>
class media
{
protected:
char title[50];
float price;
public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
void book::display()
{
cout<<"\n Title:"<<title;
cout<<"\n Pages:"<<pages;
cout<<"\n Price:"<<price;
}
void tape::display()
{
cout<<"\n Title:"<<title;
cout<<"\n Play time:"<<time<<"\t Minus";
cout<<"\n Price:"<<price;
}
int main()
{
char *title=new char[30];
float price,time;
int pages;
clrscr();
cout<<"\n Enter book Details\n";
cout<<"\n Title:";
cin>>title;
cout<<"\n Price:";
cin>>price;
cout<<"\n Pages:";
cin>>pages;
book book1(title,price,pages);
tape tape1(title,price,time);
media* list[2];
list[0]=&book1;
list[1]=&tape1;
cout<<"\n Media Details";
cout<<"\n ............Book..........";
list[0]->display();
cout<<"\n ------------Tape-----------";
list[1]->display();
return 0;
}*/
/*#include<iostream.h>
#include<conio.h>
int main()
{
int count=0;
char c;
clrscr();
cout<<"\n Input Text\n";
cin.get(c);
while(c!='\n')
{
cout.put(c);
count++;
cin.get(c);
}
cout<<"\n Number of Character="<<count<<"\n";
return 0;
}
*/
//Write()
/*
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
char *st2="Programing";
clrscr();
int n=strlen(st2);
for(int i=1;i<n;i++)
{
cout.write(st2,i);
cout<<"\n";
}
for(i=n;i>0;i--)
{
cout.write(st2,i);
cout<<"\n";
}
return 0;
}*/
/* #include<iostream.h>
#include<conio.h>
int main()
{
int items[4]={10,5,12,15};
int cost[4]={100,50,24,60};
clrscr();
cout.width(8);
cout<<"Items";
cout.width(10);
cout<<"Cost";
cout.width(15);
cout<<"TotalValue"<<"\n";
int sum=0;
for(int i=0;i<4;i++)
{
cout.width(5);
cout<<items[i];
cout.width(10);
cout<<cost[i];
int value=items[i]*cost[i];
cout.width(15);
cout<<value<<"\n";
sum=sum+value;
}
cout<<"\n Grand Total=";
cout.width(2);
cout<<sum<<"\n";
return 0;
} */
/* #include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
#include<conio.h>
class inven
{
char name[10];
int code;
float cost;
clrscr();
public:
void readdata(void);
void writedata(void);
};
void inven::readdata(void)
{
cout<<"\n Enter Name:";
cin>>name;
cout<<"\n Enter Code:";
cin>>code;
cout<<"\n Enter Cost:";
cin>>cost;
}
void inven::writedata(void)
{
cout<<setiosflags(ios::left)<<setw(10)<<name;
cout<<setiosflags(ios::right)<<setw(10)<<code;
cout<<setprecision(2)<<setw(10)<<cost<<endl;
}
int main()
{
clrscr();
inven item[3];
fstream file;
file.open("Stock.dat",ios::in|ios::out);
file.seekg(0);
cout<<"\nOutput\n\n";
for(i=0;i<3;i++)
{
file.read((char *) & item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
return 0;
} */
/* #include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int number[9]={10,20,30,40,50,60,70,80,90};
if(argc!=3)
{
cout<<"Argc="<<argc<<"\n";
cout<<"Error in arguments\n";
exit(1);
}
ofstream fout1,fout2;
fout1.open(argv[1]);
if(fout1.fail())
{
cout<<"Could Not open the file";
cout<<argv[1]<<"\n";
exit(1);
}
fout2.open(argv[2]);
if(fout2.fail())
{
cout<<"Could not open the file";
cout<<argv[2]<<"\n";
exit(1);
}
for(int i=0;i<9;i++)
{
if(number[i]%2==0)
fout2<<number[i]<<" ";
else
fout1<<number[i]<<" ";
}
fout1.close();
fout2.close();
ifstream fin;
char ch;
for(i=1;i<argc;i++)
{
fin.open(argv[i]);
cout<<"Contents of"<<argv[i]<<"\n";
do
{
fin.get(ch);
cout<<ch;
}
while(fin);
cout<<"\n\n";
fin.close();
}
return 0;
} */