Вы находитесь на странице: 1из 70

C++ Programs

- developed by Bjrane stroustrup at Bell Laboratories in USA.

- OOPS Concept - object oriented programming structure

1. Object - instance of a class.


2. Class - consist of data members and member functions
3. Data Encapsulation
4. Data Hiding
5. Inheritance - a class called derived class which can access the data
members and member functions of other class called
base class
6. Polymorphism - ability to take more than one form.
a. Compile time Polymorphism - function overloading,
Operator overloading.
b.Run time Polymorphism - Virtual functions

eg:
class stud
{
protected :
int regno,m1,m2; // data members
public :
void get(); // member function
}
class total : protected stud
{
protected:
int total;
}
class avg : protected total
{
}
void main()
{
stud s,t;
s.get();
t.get();
}

header file

-------------
#include<iostream.h>
datatypes
-----------
int (integer)
float
char
double

input syntax
--------------
cin>>var1>>var2>>.........>>varn;

eg :
int n;
char a;
cin>>n>>a; or cin>>n; cin>>a;

output syntax
-----------------
cout<<var1<<var2<<........<<varn;

eg:
int n=10;
char a='x';
cout<<" Value of n = "<<n;
cout<<"\n Value of a = "<<a;
output:
Value of n = 10
Value of a = x

Escape Sequences - is a non printing character which affects the printing


process
\a - alram or bell
\n - new line
\t - tab
\" - "
\' - '
\r - carriage return

operators
----------
1.Arithmetic operators +,-,*,/ , % (mod)
2.Relational operators > , < , <= , >=
3.Logical operators (or) || , (and) && , (not) !
4.Equality operators (equal to) == , (not equal to) !=
5.Assignment operators =, +=, -=, *=, /=, %=
6.Bitwise operators (or) | , (and) &
7.Conditional operator (condition)? thenpart : elsepart;
8.Increment Decrement ++ , --

comment - brief explanation of the program (comment lines are not executed)
1. Single line comment - // comment
2. Multi Line Comment - /* comments ................
........*/

Prog -1

// C++ program to add two numbers


#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
cout<<"\n Enter two numbers : ";
cin>>a>>b;
c = a + b;
cout<<"\n"<<a<<" + "<<b<<" = "<<c;
c = a - b;
cout<<"\n"<<a<<" - "<<b<<" = "<<c;
c = a * b;
cout<<"\n"<<a<<" * "<<b<<" = "<<c;
c = a / b;
cout<<"\n"<<a<<" / "<<b<<" = "<<c;
c = a % b;
cout<<"\n"<<a<<" % "<<b<<" = "<<c;
getch();
}

Prog -2

//Constant
#include<iostream.h>
#include<conio.h>
void main()
{
int n=100;
const float pie=3.14;
clrscr();
n=200;
pie=7.44;
cout<<"\n The value of n is :"<<n; //modify
cout<<"\n The value of pie is :"<<pie; //can not modify
getch();
}

Prog -3

//constant pointers
#include<iostream.h>
#include<conio.h>
void xstrcpy(char *,const char *);
void main()
{

clrscr();
char str1[]="nagpur"; //n=2
char str2[10];
xstrcpy(str2,str1);
cout<<endl<<str2;
getch();
}
void xstrcpy(char *t,const char *s)
{
// *s="good"; //cannot modify
while(*s!='\0')
{
*t=*s;
cout<<endl<<*t;
t++;
s++;
}
*t='\0';
}

Prog -4

//Constant References
#include<iostream.h>
#include<conio.h>
void change(const int &);
void main()
{ //int a=10,*p
int i=32; //p=&a;
clrscr();
change(i);
cout<<endl<<i;
getch();
}
void change(const &j) //&j=i;
{
// j=30; //can not modify a constant object
cout<<j;
}

Prog -5

//Biggest of three no's using default arguments


#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void maxi(int a=10,float b=98,int c=19);
void main()
{
clrscr();
maxi(25,90.6,67);
maxi(78,12.5);
maxi(10);
maxi();
getch();
}
void maxi(int a,float b,int c) //25 90.6 67
{
float d;
d=a>b?a:b; //25>90.6
d=d>c?d:c; //90.6>67
cout<<endl<<setw(5)<<d<<"is the biggest no";
}

Prog -6
//Reference variable. A reference is like a pointer
#include<iostream.h>
#include<conio.h>
void main()
{
int i=10;
int &j=i; //& - Reference datatype
clrscr();
cout<<endl<<"i="<<i<<"\t j="<<j;
j=20;
cout<<endl<<"i="<<i<<"\t j="<<j;
i=30;
cout<<endl<<"i="<<i<<"\t j="<<j;
i++;
cout<<endl<<"i="<<i<<"\t j="<<j;
j++;
cout<<endl<<"i="<<i<<"\t j="<<j;
cout<<endl<<"The address of i="<<&i;
cout<<endl<<"The address of j="<<&j;
getch();
}

Prog -7

//Swap two values through values,pointers,references


#include<iostream.h>
#include<conio.h>
void swapv(int,int);
void swapr(int &,int &);
void swapp(int *,int *);
void main()
{
int a=10,b=20;
clrscr();
swapv(a,b);
cout<<endl<<"Call by value :";
cout<<endl<<"Result of a:"<<a<<"\t"<<"Result of b:"<<b;
swapp(&a,&b);
cout<<endl<<"Call by Address :";
cout<<endl<<"Result of a:"<<a<<"\t"<<"Result of b:"<<b;
a=10;b=20;
swapr(a,b);
cout<<endl<<"Call by Reference :";
cout<<endl<<"Result of a:"<<a<<"\t"<<"Result of b:"<<b;
getch();
}
void swapv(int i,int j)
{
int t;
t=i;

i=j;
j=t;
cout<<"Inside the function :"<<endl;
cout<<i<<"\t"<<j;
}
void swapp(int *i,int *j)
{
int t;
t=*i;
*i=*j;
*j=t;
}
void swapr(int &i,int &j) //&i=a;&j=b;
{
int t;
t=i;
i=j;
j=t;
}

Prog -8

//Scope Resolution Operator:: used to access global variable


#include<iostream.h>
#include<conio.h>
int a=10;
void main()
{
int a=15;
clrscr();
cout<<"\n Local a="<<a<<"\t Global a="<<::a;
::a=20;
cout<<"\n Local a="<<a<<"\t Global a="<<::a;
getch();
}

Prog -9

//Scope Resolution Operator-2


#include<iostream.h>
#include<conio.h>
#define c cout
int i=10;
void main()
{
int i=20;
clrscr();
c<<endl<<"Local variable :"<<i<<"\t Global variable :"<<::i;
{
int i=30;
::i=40;
c<<"\n Local variable :"<<i<<"\t Global variable :"<<::i;
}
c<<"\nLocal variable :"<<i<<"\t Global variable :"<<::i;
getch();
}

Prog -10

//Reference for String


#include<iostream.h>
#include<conio.h>
void main()
{
char *str1="Rain Rain Here Again";
char *&str2=str1;
clrscr();
cout<<endl<<str1<<endl<<str2;
str1[0]='M';
cout<<endl<<str1<<endl<<str2;
str2[5]='P';
cout<<endl<<str1<<endl<<str2;
getch();
}

Prog -11

//String function
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[]="welcome";
int a[]={1,2,3},l;
clrscr();
strupr(str);
cout<<"In Upper case :"<<str<<endl;
strlwr(str);
cout<<"In Lower case :"<<str<<endl;
strrev(str);
cout<<"In reverse :"<<str<<endl;
l=strlen(str);
cout<<"Length of the string :"<<l<<endl;
cout<<"Integer array :"<<a[2];
getch();
}

Prog -12

/*Inline function - is a function which is used to fetch the function coding


at calling place */

#include<iostream.h>
#include<conio.h>
inline float square (float y)
{
return y*y;
}
void main()
{
clrscr();
float b=3.0,d;
d=square(++b);
cout<<endl<<"Using Inline function :"<<d;
getch();
}

Prog -13

//Inline Function-2
#include<iostream.h>
#include<conio.h>
#include<math.h>
inline double power(double m,int n)
{
return (pow(m,n));
}
void main()
{
double m,p;
int n;
clrscr();
cout<<"Enter the Base value :";
cin>>m; //2
cout<<"Enter the power value :";
cin>>n; //3
p=power(m,n);//2,3
cout<<"The Exponent value is :"<<p;
getch();
}

Prog -14

//Inline Function-3
#include<iostream.h>
#include<conio.h>
inline int large(int x,int y,int z)
{
if(x>y && x>z) //5>7 5>4
return x;
else if(y>z) //7>4
return y;
else
return z;
}
void main()
{
int a,b,c,d;
clrscr();
cout<<"\n Enter Three no's :"<<endl;
cin>>a>>b>>c;
d=large(a,b,c); //5 7 4
cout<<"The largest among the given no's is :"<<d;
getch();
}

Prog -15

/*
Class Syntax
------------
class <classname>
{
mode :
<datatype1> var11,var12,....,var1n;
<datatype2> var21,var22,....,var2n;
.
.
.
<datatypen> varm1,varm2,.....,varmn;
mode :
<returndatatype> functionname(<dt1> par1,<dt2> par2,....,<dtn>
parn)
{
local declarations;
set of statements;
}
.
.
.
};
three modes
-------------
1.public - access to all
2.private - accessed by the class itself
3.protected - accessed by the class and its immediate successor
*/

Prog -16

// C++ program using class and objects


#include<iostream.h>
#include<conio.h>
class student
{
private :
int rollno,m1,m2,m3;
char name[15];
float tot,avg;
public :
void getdata()
{
cout<<"\n Enter the rollno,name,m1,m2 and m3 : ";
cin>>rollno>>name>>m1>>m2>>m3;
tot = m1 + m2 + m3;
avg = tot / 3;
}
void display()
{
cout<<"\n*-----------*------------*";
cout<<"\n Student Details ";
cout<<"\n*-----------*------------*";
cout<<"\n Name : "<<name;
cout<<"\n Roll no : "<<rollno;
cout<<"\n Mark1 : "<<m1;
cout<<"\n Mark2 : "<<m2;
cout<<"\n Mark3 : "<<m3;
cout<<"\n Total : "<<tot;
cout<<"\n Average : "<<avg;
cout<<"\n*-----------*------------*";
}
};
void main()
{
student s; //object
clrscr();
// cout<<s.name;
s.getdata();
s.display();
getch();
}

Prog -17

#include<iostream.h>
#include<conio.h>
class samp
{
private :
int n;
public:
void disp()
{
n=10;
cout<<"\n n = "<<n;
}
};
class sample
{
private :
int n1;
public :
void disp1()
{
n1=20;
cout<<"\n n1 = "<<n1;
}
};
void main()
{
clrscr();
samp s;
s.disp();
sample ss;
ss.disp1();
getch();

}
Prog -18

//Example for class

#include<iostream.h>
#include<conio.h>
class student
{
private :
int m1,m2,m3,m4,m5,sum;
float avg;
char name[20];
public:
void get()
{
cout<<"Enter the name :";
cin>>name;
cout<<"Enter the marks(m1,m2,m3,m4,m5):";
cin>>m1>>m2>>m3>>m4>>m5;
}
void put()
{
sum=m1+m2+m3+m4+m5;
avg=sum/5;
cout<<"Name :"<<name<<endl;
cout<<"Sum :"<<sum<<endl;
cout<<"Average :"<<avg<<endl;
}
};

void main()
{
student s; //object
clrscr();
s.get();
s.put();
getch();
}

Prog -19

//class with multiple objects


#include<iostream.h>
#include<conio.h>
class student
{
int rollno;
char name[20];
public:
void getdata()
{
cout<<"Enter ur register no:";
cin>>rollno;
cout<<"Enter ur name:";
cin>>name;
}
void display()
{
cout<<endl<<"Your name :"<<name;
cout<<endl<<"Your reg.no:"<<rollno;
}
};
void main()
{
clrscr();
student s[10];
for(int i=1;i<=4;i++)
{
s[i].getdata();
}
for(i=1;i<=4;i++)
{
s[i].display();
}
getch();
}

Prog -20

//pointer object.
#include<iostream.h>
#include<conio.h>
class abc
{
int x;
public:
void set(int a)
{
x=a;
}
void disp()
{
cout<<"\n"<<"value of x:"<< x<<"\n";
cout<<"\n"<<"Address of x :"<<&x;
}
};
void main()
{
clrscr();
abc *ptr,obj;
ptr=&obj;
ptr->set(10);
ptr->disp();
ptr->set(100);
ptr->disp();
obj.set(90);
obj.disp();
getch();
}

Prog -21

//pointer with class


#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()
{
cout<<"\t code"<<code;
cout<<"\t price"<<price;
}
};
void main()
{
clrscr();
item *p=new item[2];
item *d=p;
int x;
float y;
for(int i=0;i<2;i++)
{
cout<<"\n input code &price for item"<<i+1;
cin>>x>>y;
p->getdata (x,y);
p++;
}
for( i=0;i<2;i++)
{
cout<<"\n item \t"<<i+1;
d->show();
d++;
}
getch();
}

//Constructor:
A function having same name as class and has no return type is called
as constructor and it is called automatically when we create an object for that
class.
1. Default constructor
2. Parameterized constructor
3. Multiple constructor
4. Copy constructor
5. Dynamic constructor
6. Constructor with default argument

//Default Constructor
/* Definition:
A Constructor that accepts no parameters is called the
default constructor.*/

Prog -22

//Example program for default constructor


#include<iostream.h>
#include<conio.h>
class sample
{
private:
int n;
public:
sample() //Constructor
{
n=0;
}
void display()
{
n++;
cout<<endl<<"The value of n is "<<n;
}
};
void main()
{
clrscr();
sample i1;
i1.display();
getch();
}

Prog -23

//Parameterized Constructor
/* Definition:
A Constructor that can take parameters (or) arguments is called
parameterized constructor.*/
//Example program for parameterized constructor
#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a,b;
public:
sample(int n,int m)
{
a=n;
b=m;
}
void display()
{
cout<<endl<<"The value of a is "<<a;
cout<<endl<<"The value of b is "<<b;
}
};
void main()
{
clrscr();
sample i1(5,10);
i1.display();
getch();
}

Prog -24

//Classes and Constructors


#include<iostream.h>
#include<conio.h>
class integer
{
private:
int i;
public:
void getdata()
{
cout<<endl<<"Enter any Integer :";
cin>>i;
}
void setdata(int j)
{
i=j;
}
integer() //zero argument constructor
{
cout<<endl<<"Constructor invoked";
}
integer(int j) //One argument constructor
{
i=j;
}
void displaydata()
{
cout<<endl<<"Value of i="<<i;
}
};
void main()
{
clrscr();
integer i1(100),i2,i3;
i1.displaydata();
i2.setdata(200);
i2.displaydata();
i3.getdata();
i3.displaydata();
getch();
}
Prog -25

//Copy Constructor
#include<iostream.h>
#include<conio.h>
class code
{
int id;
public:
code()
{}
code(int a)
{
id=a;
}
code(code &x) //copy constructor
{
id=x.id;
}
void display()
{
cout<<endl<<id;
}
};
void main()
{
clrscr();
code a(100); //Object a is created & Initialized
code b(a); //copy constructor called
code c=a; // " " " again
code d; //d is created,not initialized
d=a; //copy constructor not called
a.display();
b.display();
c.display();
d.display();
getch();
}

Prog -26

/* DYANAMIC CONSTRUCTOR:
A Dynamic constructor is one,which the allocate the exactly require
memory for the data member and initialize it with the received parameter.
The memory allocation is done with the help of the new operator.
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class name
{
char * starname;
int rank,length;
public :
name()
{
rank=0;
length=0;
starname =new char [length+1];
}
name(char *na,int rn)
{
length=strlen(na);
starname=new char [length+1];
strcpy(starname,na);
rank=rn;
}
void display()
{
cout<<"\n Starname is "<<starname<<" "<<"rank is "<<rank;
}
};
void main()
{
clrscr();
char *star1,*star2;
int rank1,rank2;
cout<<"\n Enter star name1 and rank : ";
cin>>star1>>rank1;
cout<<"\n Enter star name2 and rank : ";
cin>>star2>>rank2;
name n1(star1,1);
name n2(star2,2);
n1.display();
n2.display();
getch();
}

Prog -27

// Dynamic Initialization of objects


#include<iostream.h>
#include<conio.h>
class test
{
private:
int a,b,c;
public:
test()
{
a=0,b=0,c=0;
}
test(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
void show()
{
cout<<endl<<a<<" "<<b<<" "<<c;
}
};
void main()
{
clrscr();
int v1,v2,v3;
test t1,t2;
cout<<"\n Enter three values :";
cin>>v1>>v2>>v3;
t1=test(v1,v2,v3);
cout<<"\n Enter 3 values :";
cin>>v1>>v2>>v3;
t2=test(v1,v2,v3);
t1.show();
t2.show();
getch();
}

Prog -28

/*Multiple constructor
more than constructor can be used in a program is called as multiple
constructor*/

#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a,b;
public:
sample()
{
cout<<"\n Constructor1 is invoked";
}
sample(int a)
{
cout<<"\n Value of a is "<<a;
}
sample(int a,int b)
{
cout<<"\n Value of a & b is "<<a<<"\t"<<b;
}
};
void main()
{
clrscr();
sample s,s1(6),s2(10,20);
/* sample s1(6);
sample s2(6,10);*/
getch();
}

Prog -29

// constructor with default arguments


#include<iostream.h>
#include<conio.h>
class hello
{
private:
float a,b,c;
public:
hello(float x=1.1,float y=2.2,float z=3.3) //Priority Right to left
{
a=x;
b=y;
c=z;
}
void display()
{
cout<<endl<<a<<" "<<b<<" "<<c;
}
};
void main()
{
clrscr();
hello h1;
hello h2(6.6);
hello h3(4.4,8.8);
hello h4(2.3,4.5,2.6);
h1.display();
h2.display();
h3.display();
h4.display();
getch();
}

Prog -30

/* Destructor :
We declare destructor in a class to releases memory space for
future use.
1. Destructor is used to destroy the object that has been created by
the constructor.
2. Destructor member function name is same as the class name.
It is proceeded by Dilte "~" symbol.
3. Destructor never takes argument and nor return values.
4. It is invoke implicitly by the compiler upon exit of the program.

/* Destructor
- is a function which is having the same class name with
tilted (~) symbol and it is called whenever an object is
deleted.
- it doesn't return any values
- purpose is to close the files and deallocate the memory
*/

// C++ Program using destructor


#include<iostream.h>
#include<conio.h>
class sample
{
private :
int n;
public:
sample() // default constructor
{
n = 1;
}
~sample() // destructor
{
cout<<"\n Sample class destructor called";
}
void disp()
{
cout<<"\n n = "<<n;
}
};
class sample1
{
private :
int m;
public:
sample1() // default constructor
{
m = 2;
}
~sample1() // destructor
{
cout<<"\n Sample1 class destructor called";
}
void disp()
{
cout<<"\n m = "<<m;
}
};
void main()
{
clrscr();
sample s;
s.disp();
sample1 s1;
s1.disp();
getch();
}

Prog -31

/* Types of Inheritance:
1.Single Inheritance
2.Multiple "
3.Multi level "
4.Hierarchical "
5.Hybrid "
6.Multi-path "

Single Inheritance:
Derivation of a class from only one base class is called single
inheritance.
*/
#include<iostream.h>
#include<conio.h>
class sample1
{
private :
int n;
public:
void display1()
{
n = 10;
cout<<"\n n = "<<n;
}
};
class sample2 : public sample1
{
private :
int m;

public :
void display2()
{
sample1::display1();
m = 20;
cout<<"\n m = "<<m;
}
};
void main()

{
sample1 ss1; //base class object
sample2 ss; //derived " "
clrscr();
ss.display2();
ss1.display1();
getch();
}

Prog -32

/* Multi level inheritance :

Derivation of a class from another derived class is called


Multi level inheritance.
*/
/* Protected mode:
The members of a class declared as protected can be access the
private data and act as public within the class and act as private from
outside of the class(main function) */

// C++ Program for Multi-level Inheritance


#include<iostream.h>
#include<conio.h>
class base
{
private:
int n;
public:
void disp()
{
n=1;
cout<<"\n base class ";
cout<<"\n n = "<<n;
}
};
class derv1 : protected base
{
protected :
int m;
public :
void disp1()
{
base::disp();
m=2;
cout<<"\n\n dervied class1";
cout<<"\n base class n = "<<n;
cout<<"\n m = "<<m;
}
};
class derv2 : protected derv1
{
protected :
int p;
public :
void disp2()
{
// base::disp();
derv1::disp1();
p=3;
cout<<"\n\n dervied class 2";
cout<<"\n dervied class1 : m ="<<m;
cout<<"\n p = "<<p;
}
};
void main()
{
clrscr();
derv2 d;
d.disp2();
getch();
}

Prog -33

/* Multiple Inheritance:
Derivation of class from several (more than one) base classes is
called Multiple Inheritance.
*/

// C++ Program for multiple inheritance


#include<iostream.h>
#include<conio.h>
class base1
{
private:
int n;
public:
void disp()

{
n=1;
cout<<"\n base1 class ";
cout<<"\n n = "<<n;
}
};
class base2
{

private :
int m;
public :
void disp1()
{
m=2;
cout<<"\n\n base2 class";
cout<<"\n m = "<<m;
}
};
class derv : public base1, public base2
{
private :
int p;
public :
void disp2()
{
base1::disp();
base2::disp1();
p=3;
cout<<"\n\n dervied class";
cout<<"\n p = "<<p;
}
};
void main()
{
clrscr();
derv d;
d.disp2();
getch();
}

Prog -34

/* Hierarchical inheritance:
One base class and more than one derived class is called as
Hierarchical Inheritance.*/

//C++ Program for hierarchical inheritance


#include<iostream.h>
#include<conio.h>
class a
{
private:
int n;
public:
void disp()
{
n=1;
cout<<"\n A class ";
cout<<"\n n = "<<n;
}
};
class b : public a
{
private :
int m;
public :
void disp1()
{
cout<<"\n B class";
a::disp();
m=2;
cout<<"\n m = "<<m;
}
};
class c:public a
{
private :
int p;
public :
void disp2()
{
cout<<"\n C class";
a::disp();
p=3;

cout<<"\n p = "<<p;
}
};
class d : public a
{
private :
int q;
public :
void disp3()
{
cout<<"\n\n D class";
a::disp();
q=4;
cout<<"\n q = "<<q;
}
};
class e : public c
{
private :
int r;
public :
void disp4()
{
cout<<"\n\n E class";
c::disp2();
r=5;
cout<<"\n r = "<<r;
}
};
class f : public c
{
private :
int s;
public :
void disp5()
{
cout<<"\n\n F class";
c::disp2();
s=6;
cout<<"\n s = "<<s;
}
};
class g : public c
{
private :
int x;
public :
void disp6()
{
cout<<"\n\n G class";
c::disp2();
x=6;
cout<<"\n x = "<<x;
}
};

void main()
{
clrscr();
e ee;
f ff;
g gg;
b bb;
d dd;
ee.disp4();
ff.disp5();
gg.disp6();
bb.disp1();
dd.disp3();
getch();
}

Prog -35
/* Hybrid Inheritance :
Combination of more than one type of inheritance is called as
Hybrid inheritance.*/

// C++ Program for hybrid inheritance


#include<iostream.h>
#include<conio.h>
class a
{
private:
int n;
public:
void disp()
{
n=1;
cout<<"\n A class ";
cout<<"\n n = "<<n;
}
};
class b : public a
{
private :
int m;
public :
void disp1()
{
cout<<"\n B class";
a::disp();
m=2;
cout<<"\n m = "<<m;
}
};
class c:public a
{
private :
int p;
public :
void disp2()
{
cout<<"\n C class";
a::disp();
p=3;

cout<<"\n p = "<<p;
}
};
class d : public b
{
private :
int q;
public :
void disp3()
{
cout<<"\n\n D class";
b::disp1();
q=4;
cout<<"\n q = "<<q;
}
};
class e : public b
{
private :
int r;
public :
void disp4()
{
cout<<"\n\n E class";
b::disp1();
r=5;
cout<<"\n r = "<<r;
}
};
class f : public c
{
private :
int s;
public :
void disp5()
{
cout<<"\n\n F class";
c::disp2();
s=6;
cout<<"\n s = "<<s;
}
} ;
void main()
{
clrscr();
d dd;
e ee;
f ff;
dd.disp3();
ee.disp4();
ff.disp5();
getch();
}

Prog -36

/* Multi path inheritance:


Derivation of a class from other derived classes,which are derived from
the base class is called multipath inheritance.
*/

// C++ Program for multi path inheritance

#include<iostream.h>
#include<conio.h>
class a
{
private:
int n;
public:
void disp()
{
n=1;
cout<<"\n A class ";
cout<<"\n n = "<<n;
}
};
class b : public a
{
private :
int m;
public :
void disp1()
{
cout<<"\n B class";
a::disp();
m=2;
cout<<"\n m = "<<m;
}
};
class c:public a
{
private :
int p;
public :
void disp2()
{
cout<<"\n C class";
a::disp();
p=3;

cout<<"\n p = "<<p;
}
};
class d : public b,public c
{
private :
int q;
public :
void disp3()
{
cout<<"\n\n D class";
b::disp1();
c::disp2();
q=4;
cout<<"\n q = "<<q;
}
};
void main()
{
clrscr();
d dd;
dd.disp3();
getch();
}

Prog -37

//Function Overloading
#include<iostream.h>
#include<conio.h>
class emp
{
int eno;
char ename[10];
float hra,ta,basic_sal,tot;
public:
void get()
{
cout<<"Enter the Employee number :";
cin>>eno;
cout<<"Enter the Employee Name :";
cin>>ename;
}
void get(float h,float t,float b)
{
hra=h;
ta=t;
basic_sal=b;
}
void process()
{
tot=hra+ta+basic_sal;
}
void put()
{
cout<<"Employee Number is :"<<eno<<endl;
cout<<"Employee name is :"<<ename<<endl;
}
void put1()
{
cout<<"Employee Net Salary is :"<<tot<<endl;
}
};
void main()
{
emp e1,e2;
clrscr();
e1.get();
e1.put();
e2.get(200,40,2100);
e2.process();
e2.put1();

getch();
}

Prog -38

//Function Overloading with different datatypes

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

void add(int a,int b)


{
cout<<endl<<(a+b)*2;
}
void add(float x,float y)
{
cout<<endl<<(x+y)*2;
}
void add(int a,int b,int c)
{
cout<<endl<<(a+b+c)*3;
}
void add(float a,int b)
{
cout<<endl<<(a+b);
}

void main()
{
clrscr();
int m,n,o;
float d,e;
cout<<"\n Enter three integer values :";
cin>>m>>n>>o;
cout<<"\n Enter two floating values :";
cin>>d>>e;
add(m,n);
add(m,n,o);
add(d,m);
add(d,e);
getch();
}

Prog -39

//Function Overloading-3
#include<iostream.h>
#include<conio.h>
double power(double,int);
int power(int,int);
void main()
{
double m,p;
int n,x;
clrscr();
cout<<"\n Enter the Base value :";
cin>>m;
cout<<"\n Enter the power value:";
cin>>n;
p=power(m,n);
cout<<endl<<"The I exponent value is :"<<p<<endl;
x=power(n,m);
cout<<endl<<"The II exponent value is :"<<x<<endl;
getch();
}
double power(double i,int j)
{
int k;
int t=i,x;
for(k=1;k<j;k++)
{
x=t;
i=i*x;
}
return(i);
}
int power(int i,int j)
{
int k;
int t=j,x;
for(k=1;k<i;k++)
{
x=t;
j=j*x;
}
return(j);
}

Prog -40

/* Friend function
The main concept of the object oriented programming are data hiding
and data encapsulation.whenever data objects are declared in a private and
protected category of class,these members are restricted from accessing by
non member function.
To solve this problem,a friend function can be declared to have an
access to these members.

Definition :

A friend function is a non member function that allows the function to


access private data.The function declared with a keyword friend is called
friend function.
The keyword friend inform the compiler that it is not a member function
of the class.This function is defined elsewhere in the program.
one of the most common reason for using this function is to access
two separate classes.

The characteristics of friend functions are :

1.A function can be declared as a friend in any member of classes.


2.The friend functions are non-member functions.
3.It has full rights to the private members of the class.
4.It can be declared either in public or private.
5.Friend functions are shared by the different classes.
*/

Prog -35

//friend classes
#include<iostream.h>
#include<conio.h>
class two;
class one
{
private :
int i;
public :
one()
{
i=10;
}
friend two;
};
class two //friend class
{
public :
void func1(one o)
{
cout<<endl<<o.i;
}
void func2(one o)
{
cout<<endl<<o.i+10;
}

};
void main()
{
clrscr();
one o;
two b;
b.func1(o);
b.func2(o);
getch();
}

Prog -41
//To find minimum of two no's between two class objects using Friend function

#include<iostream.h>
#include<conio.h>
class sum1; //dummy class declaration
class sum
{
private :
int n;
public :
void get()
{
cout<<"\n Enter the value of n :";
cin>>n;
}
friend void minimum(sum obj,sum1 obj1);
};
class sum1
{
private :
int m;
public :
void get1()
{
cout<<"\n Enter the value of m :";
cin>>m;

}
friend void minimum(sum obj,sum1 obj1);
};

void minimum(sum obj,sum1 obj1)


{
int a,b;
a=obj.n;
b=obj1.m;
if(a<b)
cout<<endl<<a<<" is minimum no";
else
cout<<endl<<b<<" is minimum no";
}
void main()
{
clrscr();
sum obj;
sum1 obj1;
obj.get();
obj1.get1();
minimum(obj,obj1);
getch();
}

Prog -42

//Friend function

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

class sum
{
private :
int n;
public :
sum()
{
n=10;
}
friend void display(sum obj);
};

void display(sum obj) //non member function


{
cout<<"\n Value of n is :"<<obj.n;
}
void main()
{
clrscr();
sum obj;
display(obj);
getch();
}

Prog -43

//To accesss the values from two different class using Friend function

#include<iostream.h>
#include<conio.h>
class sum1; //dummy class declaration
class sum
{
private :
int n;
public :
sum()
{
n=10;
}
friend void display(sum obj,sum1 obj1);
};
class sum1
{
private :
int m;
public :
sum1()
{
m=20;
}
friend void display(sum obj,sum1 obj1);
};

void display(sum obj,sum1 obj1)//non-member function


{
int s1,s2;
s1=obj.n;
s2=obj1.m; // s=(s1>s2)?s1:s2;
cout<<"\n Addition of 2 no's is :"<<s1+s2;
}
void main()
{
clrscr();
sum obj;
sum1 obj1;
display(obj,obj1);
getch();
}

Prog -44

//Function Overloading
#include<iostream.h>
#include<conio.h>
class emp
{
int eno;
char ename[10];
float hra,ta,basic_sal,tot;
public:
void get()
{
cout<<"Enter the Employee number :";
cin>>eno;
cout<<"Enter the Employee Name :";
cin>>ename;
}
void get(float h,float t,float b)
{
hra=h;
ta=t;
basic_sal=b;
}
void process()
{
tot=hra+ta+basic_sal;
}
void put()
{
cout<<"Employee Number is :"<<eno<<endl;
cout<<"Employee name is :"<<ename<<endl;
}
void put1()
{
cout<<"Employee Net Salary is :"<<tot<<endl;
}
};
void main()
{
emp e1,e2;
clrscr();
e1.get();
e1.put();
e2.get(200,40,2100);
e2.process();
e2.put1();
getch();
}

Prog -45

//Function Overloading with different datatypes

#include<iostream.h>
#include<conio.h>
void add(int a,int b)
{
cout<<endl<<(a+b)*2;
}
void add(float x,float y)
{
cout<<endl<<(x+y)*2;
}

void add(int a,int b,int c)


{
cout<<endl<<(a+b+c)*3;
}
void add(float a,int b)
{
cout<<endl<<(a+b);
}

void main()
{
clrscr();
int m,n,o;
float d,e;
cout<<"\n Enter three integer values :";
cin>>m>>n>>o;
cout<<"\n Enter two floating values :";
cin>>d>>e;
add(m,n);
add(m,n,o);
add(d,m);
add(d,e);
getch();
}

Prog -46

//Function Overloading-3
#include<iostream.h>
#include<conio.h>
double power(double,int);
int power(int,int);
void main()
{
double m,p;
int n,x;
clrscr();
cout<<"\n Enter the Base value :";
cin>>m;
cout<<"\n Enter the power value:";
cin>>n;
p=power(m,n);
cout<<endl<<"The I exponent value is :"<<p<<endl;
x=power(n,m);
cout<<endl<<"The II exponent value is :"<<x<<endl;
getch();
}
double power(double i,int j)
{
int k;
int t=i,x;
for(k=1;k<j;k++)
{
x=t;
i=i*x;
}
return(i);
}
int power(int i,int j)
{
int k;
int t=i,x;
for(k=1;k<j;k++)
{
x=t;
i=i*x;
}
return(i);
}

Prog -47

/* OPERATOR OVERLOADING

Giving an additional task to a predefined operator is called operator


overloading.

Syntax:
returntype operator symbol(arg)
{
};
*/
//Example for unary operator overloading

#include<iostream.h>
#include<conio.h>
class over
{
int count;
public:
void operator ++();
void operator --();
over()
{
count=0;
}
void disp()
{
cout<<"\n count ="<<count;
}
};
void over :: operator ++()
{
count++;
}
void over :: operator --()
{
count--;
}
void main()
{
clrscr();
over obj1,obj2;
obj1.disp();
obj2.disp();
obj1++;
++obj1;
obj2++;
++obj2;
cout<<"\n ";
obj1.disp();
obj2.disp();
obj1--;
--obj2;
cout<<"\n ";
obj1.disp();
obj2.disp();
getch();
}

Prog -48

//Binary Operator overloading -two operands


#include<iostream.h>
#include<conio.h>
class binary
{
int a,b;
public :
binary operator -(binary);
binary operator /(binary);
binary()
{
a=0;b=0;
}
binary(int x,int y)
{
a=x;b=y;
}
void disp()
{
cout<<"a="<<a<<" "<<"b="<<b;
}
};
binary binary :: operator -(binary b2)
{
binary temp;
temp.a=a-b2.a;
temp.b=b-b2.b;
return(temp);
}
binary binary :: operator /(binary b2)
{
binary temp;
temp.a=a/b2.a;
temp.b=b/b2.b;
return(temp);
}
void main()
{
clrscr();
binary b1(100,20);
binary b2(10,5);
binary b3;
b3=b1-b2;
cout<<"\n Subtraction :";
b3.disp();
b3=b1/b2;
cout<<"\n Division :";
b3.disp();
getch();
}
Prog -49

//Binary operator overloading using string


#include<iostream.h>
#include<conio.h>
#include<string.h>
class hello
{
char st1[50];
public:
hello()
{
strcpy(st1,"\0");
}
hello(char s[])
{
strcpy(st1,s);
}
void disp()
{
cout<<"\n"<<st1;
}
hello operator + (hello t)
{
hello temp;
strcat(temp.st1,st1);
strcat(temp.st1,t.st1);
return(temp);
}
};
void main()
{
clrscr();
hello str1("c++");
hello str2("Java");
hello str3("programming");
hello str4;
str4=str1+str2;
str4.disp();
cout<<"\n Second Term :"<<endl;
str4=str4+str3;
str4.disp();
getch();
}

Prog -50

/* Rules for Operator Overloading :

1.We can't pass any argument for unary operator overloading.


2.In binary operator overloading we must pass one argument explicitly,
another one is take implicitly.
3.By using friend function,we must give one argument for unary operator
overloading and two arguments for binary operator overloading.
4.We can't use some operators in operator overloading.
Eg: !=,?,:,&,->*
*/
// Binary Operator Overloading using friend functions
#include<iostream.h>
#include<conio.h>
class sample
{
int weight,height;
public:
sample()
{
weight=0;
height=0;
}
sample(int n,int h)
{
weight=n;
height=h;
}
void show()
{
cout<<"\n"<<"Weight ="<<weight;
cout<<"\n"<<"Height ="<<height;
}
friend sample operator +(sample s1,sample s2);
};
sample operator +(sample s1,sample s2) //non-member function
{
sample temp;
temp.weight=s1.weight+s2.weight;
temp.height=s1.height+s2.height;
return(temp);
}
void main()
{
clrscr();
sample s1(50,170);
sample s2(65,173);
sample s3;
s3=s1+s2;
s3.show();
getch();
}

Prog -51

// Unary Operator Overloading using friend functions


#include<iostream.h>
#include<conio.h>
class over
{
int count;
public:
over()
{
count=0;
}
over(int a)
{
count=a;
}
friend over operator ++(over ob);
void disp();
};
over operator ++(over ob)
{
over temp;
temp.count=++ob.count;
return(temp);
}
void over :: disp()
{
cout<<"\n"<<count;
}
void main()
{
clrscr();
over ob(10);
over ob2;
ob2=++ob;
ob.disp();
ob2.disp();
getch();
}

Prog -52

/* static member function


1. A static member function also declared with the keyword static.
2. The static member function can access only static data member
of its class.
3. A static member function is not part of objects of a class.
4. The static member function can be accessed directly by using the
class name and scope resolution operator.

Syntax:

class_name::function_name;
*/

//Example for static member data and static member function


#include<iostream.h>
#include<conio.h>
class sample
{
private :
static int count; //static data member
public :
sample()
{
count++;
}
static void disp() //static member function
{
cout<<"The value of count is :"<<count<<endl;
}
};
int sample::count=0; //static data definition
void main()
{
clrscr();
cout<<"\n\t\t Before creation of object "<<endl;
sample::disp();
sample s1,s2,s3,s4;
cout<<"\n\t\t After creation of object "<<endl;
sample::disp();
getch();
}

Prog -53

/* Virtual Base class


If a class is derived from already derived class to access a
particular variable in both derived class is called virtual base class.
Syntax:
class derivename : virtual public base*/

#include<iostream.h>
#include<conio.h>
class base
{
protected :
int x;
public :
void set1(int a)
{
x=a;
}
};
class derv1 : virtual public base
{
protected :
int y;
public :
void set2(int b)
{
y=b;
}
void show()
{
cout<<"\n Value of X ="<<x;
cout<<"\n Value of Y ="<<y;
}
};
class derv2 : virtual public base
{
protected :
int z;
public :
void set3(int c)
{
z=c;
}
void view()
{
cout<<"\n Value of X ="<<x;
cout<<"\n Value of Z ="<<z;
}
};
class virderv : public derv1,public derv2
{
protected :
int m;
public :
void set4(int n)
{
m=n;
}
void disp()
{
cout<<"\n Value of X ="<<x;
cout<<"\n Value of Y ="<<y;
cout<<"\n Value of Z ="<<z;
cout<<"\n Value of M ="<<m;
show();
view();
}
};
void main()
{
clrscr();
virderv obj1;
obj1.set1(35);
obj1.set2(45);
obj1.set3(55);
obj1.set4(85);
obj1.disp();
getch();
}

Prog -54

/* Rules for Virtual Functions :

1. The virtual functions must be members of some class.


2. They cannot be static members.
3. They are accessed by using object pointers.
4. A virtual functions can be a friend of another class.
5. A virtual function in a base class must be defined even though it may
not be used.
6. We cannot have virtual constructors but we can have virtual destructors.
7. If two functions with the same name have different prototypes,c++
considers them as overloaded functions.
8. If a virtual function is defined in a base class,it need not be
neccessarily redefined in the derived class.

*/

/* virtual function :
If a function with same name with same datatype is used in different
classes then it will access by using virtual function concept.*/

Prog -55

#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display()
{
cout<<"\n Display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
class derived : public base
{
public:
void display()
{
cout<<"\n Display derived";
}
void show()
{
cout<<"\n Show derived";
}
};
void main()
{
clrscr();
derived d;
base *bptr;
cout<<"\n bptr points to base";
bptr->display();
bptr->show();
cout<<"\n bptr points to derived";
bptr=&d;
bptr->display();
bptr->show();
/*
d.display();
d.show();*/

getch();
}

Prog -56

#include<iostream.h>
#include<conio.h>
class number
{
private:
int p;
public:
void assign(int q)
{
this->p=q;
}
void display()
{
cout<<endl<<this->p<<"\n";
}
number()
{
cout<<"\n Object created";
}
~number()
{
cout<<"\n Object destroyed";

}
};
void main()
{
clrscr();
number n,m;
n.assign(100);
m.assign(200);
n.display();
m.display();
getch();
}

Prog -57

//this pointer -current memory location or value.


#include<iostream.h>
#include<conio.h>
class xyz
{
float m;
public:
xyz()
{
m=10.99;
}
void address()
{
cout<<"\n the address is "<<this<<"\n";
}
void disp()
{
cout<<"\n address :"<<this<<"value :"<<m;
}
};
void main()
{
clrscr();
xyz obj1,obj2;
obj1.address();

obj2.address();
obj1.disp();
obj2.disp();
getch();
}

Prog -58

/* Templates :
If we want to access any datatype with same function or class, we can
use template concept.

Types of template:
1. Function template
2. Class template
syntax:
template<class type>
type functionname(type parameter1,type parameter2,...,type parametern)
{
Local declaration;
set of statements;
}

*/
#include<iostream.h>
#include<conio.h>
template<class t1,class t2>
void display(t1 x,t2 y)
{
cout<<"\n x = "<<x<<" "<<"\t y = "<<y;
}
void main()
{
clrscr();
display(1048,"abi");
display(12.25,'A');
display(10,20);
getch();
}

Prog -59

# include<iostream.h>
# include<conio.h>
template <class tmpdt>
tmpdt add(tmpdt a ,tmpdt b)
{
tmpdt temp;
temp = a+b;
cout<<"\n a = "<<a;
cout<<"\n b = "<<b;
return(temp);
}
void main()
{
clrscr();
int i=10,j=20,r1;
float x=10.1, y=30.5,r2;
char a='t', b='w',r3;
cout<<" The original value of i and j is "<<i<<" "<<j<<endl;
cout<<" The original value of x and y is "<<x<<" "<<y<<endl;
cout<<" The original value of a and b is "<<a<<" "<<b<<endl;
r1 = add(i,j);
r2 = add(x,y);
r3 = add(a,b);
cout<<"\n Integer Addition Result = "<<r1<<endl;
cout<<" Float Addition Result = "<<r2<<endl;
cout<<" Character Addition Result = "<<r3<<endl;
getch();
}

Prog -60

//Example for class template


#include<iostream.h>
#include<conio.h>
template <class t1,class t2>
class test
{
t1 a;
t2 b;
public :
test(t1 x,t2 y)
{
a=x;
b=y;
}
void show()
{
cout<<"\n a = "<<a<<" and "<<"\t b = "<<b;
}
};
void main()
{
clrscr();
test<float,int> obj1(123.25,125);
test<int,char> obj2(100,'n');
test<int,char *> obj3(102,"hai");
obj1.show();
obj2.show();
obj3.show();
getch();
}

Prog -61

/* FILE
-------
header file - #include<fstream.h>
stream - flow of data
------
1. ifstream - for reading text from the file
2. ofstream - for writing text into the file
3. fstream - for both reading and writing from/ into the file

file operations
_______________
1. file open
2. file process
3. file close

file declaration with open


-----------------------------
<stream> fileptrvar("filename",[mode]);

To read from the file


-----------------------
fileptrvar>>var1>>var2>>......>>varn;

To write into the file


------------------------
fileptrvar<<var1<<var2<<.......<<varn;

To close the file


------------------
fileptrvar.close();
*/
// C++ Program to write the content into the file
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream fp("sample11.txt");
fp<<"AAA "<<"33 "<<"101"<<endl;
fp<<"BBB "<<"55 "<<"102"<<endl;
fp<<"CCC "<<"44 "<<"103"<<endl;
fp.close();
cout<<"\n Given content is written into the file successfully";
getch();
}

Prog -62

// C++ program to read a character by character from the file


#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
ifstream in("sample11.txt");
char ch;
clrscr();
cout<<"\n File Consists \n";
while(in)
{
in.get(ch);
cout<<ch;
}
in.close();
getch();
}

Prog -63

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ofstream out("c:\\tc\\test",ios::in||ios::binary);
clrscr();
out<<"Shirts"<<" "<<625<<endl;
out<<"Jeans"<<" "<<760<<endl;
out<<"Trousers"<<" "<<600<<endl;
out.close();

ifstream in("c:\\tc\\test",ios::binary);
char item[20];
float cost;
in>>item>>cost;
cout<<item<<" "<<cost<<"\n";
in>>item>>cost;
cout<<item<<" "<<cost<<"\n";
in>>item>>cost;
cout<<item<<" "<<cost<<"\n";

in.close();
getch();
}

Prog -64
// C++ program to write records into and read records from the file
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
char nm[50];
int age;
int idnum;
clrscr();
ofstream out("samp1.txt");
out<<"AAA"<<" "<<33<<" "<<101<<endl;
out<<"BBB"<<" "<<55<<" "<<102<<endl;
out<<"CCC"<<" "<<44<<" "<<103<<endl;
out.close();
cout<<"\n Given content is written into the file successfully";
ifstream in("samp1.txt");
cout<<"\n\n File Consists \n";
in>>nm>>age>>idnum;
cout<<nm<<"\t"<<age<<"\t"<<idnum<<endl;
in>>nm>>age>>idnum;
cout<<nm<<"\t"<<age<<"\t"<<idnum<<endl;
in>>nm>>age>>idnum;
cout<<nm<<"\t"<<age<<"\t"<<idnum<<endl;
in.close();
getch();
}

Prog -65

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#define size 6
void main()
{
char num[size+1];
clrscr();
fstream inout("samp",ios::in|ios::out|ios::binary);
for (int i=0;i<10;i++)
inout<<i;
inout.seekp(2);
char var[20];
cout<<"enter a string:";
cin>>var;
inout>>var;
inout.seekg (4);
inout.read (num,size);
num[size]=0;
cout<<num<<endl;
getch();
}

Prog -66

#include<iostream.h>
#include<conio.h>
class stack
{
int top;
int item[50];
public:
stack()
{
top=0;
}
void push(int data);
void view();
void pop();
};
void main()
{
stack s;
int choice,data;
clrscr();
do
{
cout<<"\n 1. Push ";
cout<<"\n 2. Pop ";
cout<<"\n 3. View";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter the data : ";
cin>>data;
s.push(data);
break;
case 2:
s.pop();
break;
case 3:
s.view();
break;
}
}
while(choice !=4);
}
void stack :: push(int data)
{
if(top == 5)
{
cout<<"\n Stack OverFlow";
}
else
{
item[top] = data;
top = top+1; // top++;
}
}
void stack :: pop()
{
if(top == 0)
{
cout<<"\n Stack is Empty";
}
else
{
top = top -1; // ps->top --;
cout<<"\n pop the data : \n "<<item[top];
}
}

void stack :: view()


{
int i;
cout<<"\n\n stack values -----";
for(i=top-1;i>=0;i--)
cout<<"\t\n"<<item[i];
}

Prog -67

#include<iostream.h>
#include<conio.h>
class queue
{
int front,rear;
int item[50];
public:
queue()
{
front=0;
rear=0;
}
void push(int data);
void view();
void pop();
};
void main()
{
queue q;
int choice,data;
clrscr();
do
{
cout<<"\n 1. Push ";
cout<<"\n 2. Pop ";
cout<<"\n 3. View";
cout<<"\n 4. Exit";
cout<<"\n Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\n Enter the data : ";
cin>>data;
q.push(data);
break;
case 2:
q.pop();
break;
case 3:
q.view();
break;
}
}
while(choice !=4);
}
void queue :: push(int data)
{
if(rear-front== 5)
{
cout<<"\n Queue OverFlow";
}
else
{
item[rear] = data;
rear++;
}
}
void queue :: pop()
{
if(front==rear)
{
cout<<"\n Queue is Empty";
front=rear=0;
}
else
{
cout<<"\n pop the data : \n "<<item[front];
front=front+1;
}
}

void queue :: view()


{
int i;
cout<<"\n\n Queue values -----";
for(i=front;i!=rear;i++)
cout<<"\t"<<item[i];
}

Prog -68

/* Self Referential Structure - structure which refers to itself


*/
// C++ Program for linked list using self referential structure
#include<iostream.h>
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<conio.h>
struct node
{
char data[25];
struct node *link;
}*start,*first;
void main()
{
void create(struct node *p);
void display(struct node *p);
void insert();
void deletion();
int ch;
start = (struct node *)malloc(sizeof(struct node));
clrscr();
create(start);
do
{
clrscr();
cout<<"\n 1 - insertion";
cout<<"\n 2 - deletion";
cout<<"\n 3 - Display";
cout<<"\n 4 - Exit";
cout<<"\n Enter ur choice : ";
cin>>ch;
switch(ch)
{
case 1 :
insert();
display(start);
break;
case 2 :
deletion();
display(start);
break;
case 3 :
display(start);
break;
case 4 :
cout<<"\n process finished";
break;
}
getch();
}while(ch != 4);
}
void create(struct node *p)
{
cout<<"\n Enter the data (NULL to end): ";
flushall();
cin>>p->data;
if ((strcmp(p->data,"NULL")) == 0)
{
p->link = NULL;
return;
}
else
{
p->link = (struct node *)malloc(sizeof(struct node));
create(p->link);
}
}
void display(struct node *p)
{
if ((strcmp(p->data,"NULL")) != 0)
{
cout<<p->data<<" ";
display(p->link);
}
}
void insert()
{
char ch,prvdata[25];
struct node *tmp;
first = start;
tmp = (struct node*)malloc(sizeof(struct node));
cout<<"\n Enter the data to be inserted : ";
flushall();
cin>>tmp->data;
cout<<"\n Do u want to insert in the beginning (y/n) : ";
flushall();
cin>>ch;
if (ch == 'y' || ch == 'Y') // inserting data in the beginning
{
tmp->link = start;
start = tmp;
}
else // inserting data in middle or last
{
cout<<"\n Enter the previous data : ";
flushall();
cin>>prvdata;
while(strcmp(first->data,prvdata) != 0)
first = first->link;
tmp->link = first->link;
first->link = tmp;
}
}
void deletion()
{
char deldata[25];
cout<<"\n Enter the data to be deleted : ";
flushall();
cin>>deldata;
first = start;
if (strcmp(first->data,deldata) == 0)//checking first to be deleted or not
start = first->link;
else // checking for other nodes in the list
{
while(strcmp(first->link->data,deldata) != 0)
first= first->link;
first->link = first->link->link;
}
}

Prog -69

#include<iostream.h>
#include<conio.h>
class tree;
class tnode
{
friend class tree;
private :
int data;
tnode *lchild;
tnode *rchild;
public :
tnode()
{
data=0;
lchild=0;
rchild=0;
}
tnode(int d)
{
data = d;
lchild=0;
rchild=0;
}
};
class tree
{
private :
tnode *root;
public :
tree()
{
root=0;
}
void insert(int temp);
void display();
void inorder(tnode *p);
void preorder(tnode *p);
void postorder(tnode *p);
};
void tree::insert(int temp)
{

tnode *p = root;
tnode *q = 0;
while(p)
{
q = p;
if (temp == p->data)
return;
else if (temp < p->data)
p = p->lchild;
else
p = p->rchild;
}
tnode *t = new tnode;
t->lchild = 0;
t->rchild=0;
t->data = temp;
if (! root)
root = t;
else if (t->data < q->data)
q->lchild = t;
else
q->rchild = t;
return;
}
void tree::display()
{
cout<<"\n Inorder\n";
inorder(root);
cout<<"\n Preorder\n";
preorder(root);
cout<<"\n Postorder\n";
postorder(root);
}
void tree::inorder(tnode *p)
{
if (p)
{
inorder(p->lchild);
cout<<p->data<<" ";
inorder(p->rchild);
}
}
void tree::preorder(tnode *p)
{
if (p)
{
cout<<p->data<<" ";
preorder(p->lchild);
preorder(p->rchild);
}
}
void tree::postorder(tnode *p)
{
if (p)
{
postorder(p->lchild);
postorder(p->rchild);
cout<<p->data<<" ";
}
}
void main()
{
tree t;
int ch;
clrscr();
do
{
cout<<"\n 1 - Insert";
cout<<"\n 2 - Display";
cout<<"\n 3 - Exit";
cout<<"\n Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
int n,p;
cout<<"\n Enter the Number of elements : ";
cin>>n;cout<<"\n Enter the Elements : ";
for(int i=0;i<n;i++)
{
cin>>p;
t.insert(p);
}
break;
case 2:
t.display();
break;
case 3:
cout<<"\n Process Over";
break;
default:
cout<<"\n Invalid Choice";
break;
}
getch();
clrscr();
}
while(ch != 3);
getch();
}

Вам также может понравиться