Tuesday 24 February 2015

Virtual Function in C++

#include<iostream.h>
#include<conio.h>
class Base
{
public:
virtual void display()
{
cout<<"\n I am Base Class Display";
}
};
class Derived:public Base
{
public:
void display()
{
cout<<"\n I am Derived Class Display";
}
};
void main()
{
Base *b;
clrscr();
b=new Base;
b->display();
b=new Derived;
b->display();
getch();
}

File Read and Write in C++

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
main()
{
clrscr();
ofstream out("Items.txt",ios::out);

out<<"\nShirts"<<" "<<350<<endl;
out<<"\nTrousers"<<" "<<450<<endl;
out<<"\nJeans"<<" "<<750<<endl;
out.close();

ifstream in("Items.txt",ios::in);
char item[50];
float cost;
in>>item>>cost;
cout<<"\n Item is "<<item;
cout<<"\n Cost is "<<cost;
in>>item>>cost;
cout<<"\n Item is "<<item;
cout<<"\n Cost is "<<cost;
in>>item>>cost;
cout<<"\n Item is "<<item;
cout<<"\n Cost is "<<cost;

in.close();
getch();
}

This Pointer Using C++

/* This Pointer */

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

class Rectangle
{
private:
int width,height;
public:
void Create(int width,int height)
{
this->width=width;
this->height=height;
}
void Display()
{
cout<<"\n Width :"<<width;
cout<<"\n Height :"<<height;
}
};

void main()
{
clrscr();
Rectangle r1,r2;
r1.Create(100,100);
r1.Display();
r2.Create(200,200);
r2.Display();
getch();
}

+ Operator Overloading In C++

#include<iostream.h>
#include<conio.h>
class Test
{
public:
int value;
Test(int i);
void show();
Test operator+(Test &T1);
Test operator+(int i);
};
Test::Test(int i)
{
value=i;
}
void Test::show()
{
cout<<"\n The Value is :"<<value<<endl;
}
Test Test::operator+(Test &T1)
{
value=T1.value+value;
return value;
}
Test Test::operator+(int i)
{
value=value+i;
return value;
}
void main()
{
Test T2(10),T3(25);
clrscr();
T2=T2+T3;
T2.show();
T2=T3+100;
T2.show();
getch();
}

Static Function Using C++

#include<iostream.h>
#include<conio.h>
class StatDemo
{
public:
static int Square(int a)
{
return a*a;
}
static int Cube(int a)
{
return a*a*a;
}
};
main()
{
clrscr();
cout<<"\n Square of 10 is :"<<StatDemo::Square(10);
cout<<"\n Cube of   10 is :"<<StatDemo::Cube(10);
getch();
}

Static Varible Program in C++

#include<iostream.h>
#include<conio.h>
void Like();
main()
{
int i=0;
for(i=0;i<10;i++)
Like();
getch();
}
void Like()
{
static int x=100;
cout<<"\n X Variable Value is  :"<<x;
x++;
}

Stack Implementation Using C++

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class StackDemo
{
private:
struct Stack
{
int Data;
struct Stack *Next;
}*Top;
public:
StackDemo
{
Top=NULL;
}
Push()
{
Stack *Source=new Stack;

cout<<"\n Enter Data For Stack\n";
cin>>Source->Data;
Source->Next=NULL;

if(Top==NULL)
{
Top=new Stack;
Top=Source;
}
else
{
Source->Next=Top;
Top=Source;
}
}

Pop()
{
Stack *Source=new Stack;
Source=Top;
if(Source==NULL)
{
cout<<"\n Stack Is Empty";
}
else
{
cout<<"\n Poped Item is :"<<Source->Data;
Top=Top->Next;
delete Source;
}
getch();
}

Display()
{
Stack *Source=new Stack;
Source=Top ;
while(Source!=NULL)
{
cout<<"\n Stack Item is :"<<Source->Data;
Source=Source->Next;
}
getch();
}

GetChoice()
{
int choice;
Home:
clrscr();
cout<<"\n\t\t\t Stack Demo \n";
cout<<"\n\t\t\t1.. Push";
cout<<"\n\t\t\t2.. Pop";
cout<<"\n\t\t\t3.. Display";
cout<<"\n\t\t\t4.. Exit";
cout<<"\n\n\t\t Enter Your Choice\n";
cin>>choice;
switch(choice)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Display();
break;
case 4:
exit(0);
case 5:
cout<<"\n Wrong Press";
}
goto Home;
}
};
void main()
{
StackDemo d;
d.GetChoice();
}

Sine() series Program in C++

#include<iostream.h>
#include<conio.h>
#include<math.h>
long fact(long);
main()
{
long base,power,i,j;
float res=1.0;
clrscr();
cout<<"\n Enter Base Value\n";
cin>>base;
cout<<"\n Enter Power Value\n";
cin>>power;

for(i=1;i<=power;i++)
{
res+=pow(base,i)/fact(i+1);
}
cout<<"\n Result is "<<res;
getch();
}
long fact(long fact)
{
long b=fact;
if(b==0)
return 1;
else
for(long i=1;i<fact;i++)
{
b=b*i;
}
return b;
}

Shortand Assignment Operator in C++

#include<iostream.h>
#include<conio.h>
main()
{
int a=5;
clrscr();
cout<<"\n A+=5 "<<(a+=5); /* a=a+5 or a+=5 */
cout<<"\n A-=5 "<<(a-=5);
cout<<"\n A*=5 "<<(a*=5);
cout<<"\n A/=5 "<<(a/=5);
cout<<"\n A%=5 "<<(a%=5);
getch();
}

Single Inheritence in C++


/*
Single Inheritence
==================

Syntax For Inheritence
======================

class DerivedClassName:accessspecifier BaseClassName
{

}


*/
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void BaseClass()
{
cout<<"\n I am Base Class";
}
};
class Derived:public Base
{
public:
void DerivedClass()
{
cout<<"\n I am Derived Class";
}
};
main()
{
Derived d;
clrscr();
d.BaseClass();
d.DerivedClass();
getch();
}

Scope Resolution Operator in C++

/*
::  scope resolution operator
*/

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

void AccGlobe(); // Function Declaration
int a=10;

main()
{
int a=100;
clrscr();
cout<<"\n Global Variable :"<<::a;
cout<<"\n Local Variable :"<<a;
AccGlobe();
getch();
}
void AccGlobe()
{
cout<<"\n Global Variable Within Function :"<<::a;
}

Arrays Sample Program in C++

#include<iostream.h>
#include<conio.h>
main()
{
char a[6],i=0;
clrscr();

a[i]='H';
a[++i]='E';
a[++i]='L';
a[++i]='L';
a[4]='O';
a[5]='\0';

cout<<"\n A[0] :"<<a[0];
cout<<"\n A[1] :"<<a[1];
cout<<"\n A[2] :"<<a[2];
cout<<"\n A[3] :"<<a[3];
cout<<"\n A[4] :"<<a[4];

getch();
}

==========================

#include<iostream.h>
#include<conio.h>
main()
{
char a[50];
clrscr();
cout<<"\n Enter Your Name ";
cin>>a;
cout<<"\n Your Name is "<<a;
getch();
}

==============================

#include<iostream.h>
#include<conio.h>
void main()
{
int a[5];
clrscr();

a[0]=100;
a[1]=200;
a[2]=300;
a[3]=400;
a[4]=500;

cout<<"\n A[0] :"<<a[0];
cout<<"\n A[1] :"<<a[1];
cout<<"\n A[2] :"<<a[2];
cout<<"\n A[3] :"<<a[3];
cout<<"\n A[4] :"<<a[4];

getch();
}

=======================

#include<iostream.h>
#include<conio.h>
main()
{
int a[5],i;
clrscr();

cout<<"\n Enter 5 Numbers\n";
for(i=0;i<5;i++)
cin>>a[i];

cout<<"\n Given 5 Numbers\n";
for(i=0;i<=4;i++)
cout<<"\n"<<a[i];

getch();
}

Relational Operators in C++

/*

Relational Operators
====================
< - Lessthan
<= - Lessthan or Equal To
> - Greater Than
>= - Greater Than Or Equal To
== - Equal To
!= -       Not Equal To

*/

#include<stdio.h>
#include<conio.h>
main()
{
int a=10,b=5;
clrscr();
cout<<"\n A   >   B      ="<<(a>b);
cout<<"\n A   >=  10     ="<<(a>=10);
cout<<"\n A   >   10     ="<<(a>10);
cout<<"\n A   <   B      ="<<(a<b);
cout<<"\n A   <=  10     ="<<(a<=10);
cout<<"\n A   <   10     ="<<(a<10);
cout<<"\n A   ==  10     ="<<(a==10);
cout<<"\n A   ==  5      ="<<(a==5);
cout<<"\n A   !=  5      ="<<(a!=5);
cout<<"\n A   !=  10     ="<<(a!=10);
getch();
}

Register Variable in C++

#include<stdio.h>
#include<conio.h>
main()
{
register int i=100;
clrscr();
cout<<"\n Register Variable is  :"<<i;
getch();
}

Read() and Write() Functions Using C++

// Read and Write Functions

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
main()
{
int var1,n1;
float var2,n2;
clrscr();

cout<<"\n Enter an Integer Value\n";
cin>>var1;
cout<<"\n Enter an Float Value\n";
cin>>var2;

ofstream out("MyFile.txt",ios::binary);

out.write((char*)&var1,sizeof(int));
out.write((char*)&var2,sizeof(float));
out.close();

ifstream in("MyFile.txt",ios::binary);
in.read((char*)&n1,sizeof(int));
in.read((char*)&n2,sizeof(float));
cout<<n1<<endl;
cout<<n2<<endl;
out.close();
}


Class Sample Program Using C++

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

class Rectangle
{
private:
int width,height;
public:
void Create(int w,int h)
{
width=w;
height=h;
}
void Display()
{
cout<<"\n Width :"<<width;
cout<<"\n Height :"<<height;
}
};
void display();
void main()
{
Rectangle r1,r2;
r1.Create(100,100);
r1.Display();
r2.Create(200,200);
r2.Display();
display();
}
void display()
{
cout<<"\n Welcome to Function";
}

Queue Using C++

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

class QueueDemo
{
private:
struct Queue
{
int Data;
struct Queue *Next;
}*Front,*Rear;

public:
QueueDemo()
{
Front=NULL;
Rear=NULL;
}
Push()
{
Queue *Source=new Queue;

cout<<"\n Enter Data For Queue\n";
cin>>Source->Data;
Source->Next=NULL;

if(Front==NULL)
{
Front=new Queue;
Rear=new Queue;
Front=Rear=Source;
}
else
{
Rear->Next=Source;
Rear=Source;
}
}
Pop()
{
Queue *Source=new Queue;
Source=Front;

if(Source==NULL)
{
cout<<"\n Queue Is Empty";
}
else
{
cout<<"\n Poped Item is       :"<<Source->Data;
Front=Front->Next;
delete Source;
}
getch();
}
Display()
{
Queue *Source=new Queue;
Source=Front;

while(Source!=NULL)
{
cout<<"\n Queue Item is  :"<<Source->Data;
Source=Source->Next;
}
getch();
}
void ChoiceGet()
{
int choice;
Home:
clrscr();
cout<<"\n\t\t\t Queue Demo \n";
cout<<"\n\t\t\t1.. Push";
cout<<"\n\t\t\t2.. Pop";
cout<<"\n\t\t\t3.. Display";
cout<<"\n\t\t\t4.. Exit";
cout<<"\n\n\t\t Enter Your Choice\n";
cin>>choice;
switch(choice)
{
case 1:
Push();
break;
case 2:
Pop();
break;
case 3:
Display();
break;
case 4:
exit(0);
case 5:
cout<<"\n Wrong Press";
}
goto Home;
}
};
void main()
{
clrscr();
QueueDemo QD;
QD.ChoiceGet();
getch();
}

Pure Virtual Function Program in C++

#include<iostream.h>

class PureVirtual
{
public:
virtual void PDemo()=0;
virtual void NDemo(){};
};
class PureDef:public PureVirtual
{
public:
void PDemo()
{
cout<<"\n Pure Virtual Demo";
}
void NDemo()
{
cout<<"\n Null Virtual Demo";
}
};
void main()
{
PureDef PD;
PD.PDemo();
PD.NDemo();
}

Protected Keyword in C++

#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a,b;
};
class Derived:private Base
{
public:
void GetPut()
{
cout<<"\n Enter A Value :";
cin>>a;
cout<<"\n Enter B Value :";
cin>>b;
cout<<"\n A =  "<<a;
cout<<"\n B =  "<<b;
}
};
class Derived1:public Derived /* Derived.a & Derived.b is not accessible */
{
};
void main()
{
Derived d;
d.GetPut();
}
==================
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a,b;
};
class Derived:public Base
{
public:
void GetPut()
{
cout<<"\n Enter A Value :";
cin>>a;
cout<<"\n Enter B Value :";
cin>>b;
cout<<"\n A =  "<<a;
cout<<"\n B =  "<<b;
}
};
void main()
{
Derived d;
d.GetPut();
}
=====================
#include<iostream.h>
#include<conio.h>
class Base
{
protected:
int a,b;
public:
Base()
{
a=10;
b=20;
}
void Put()
{
cout<<"\n A value is :"<<a;
cout<<"\n B value is :"<<b;
}
};
class Derived:public Base
{
private:
int c;
public:
Derived()
{
c=100;
}
void Put()
{
cout<<"\n C =  "<<c;
}
};
void main()
{
Derived d;
clrscr();
d.Put();
d.Base::Put();
getch();
}

Pointer Using C++

#include<iostream.h>
#include<conio.h>
main()
{
int *p,a=100;
clrscr();
cout<<"\n Before Assigning";
cout<<"\n Address of a is      :"<<&a;
cout<<"\n Adress of p is       :"<<p;
p=&a;
cout<<"\n after Assigning";
cout<<"\n Address of a is      :"<<&a;
cout<<"\n Address of p is      :"<<p;
*p=1000;
cout<<"\n Value of a is       :"<<a;
cout<<"\n Value of p is       :"<<*p;
getch();
}

Prime Number Program in C++

#include<stdio.h>
#include<conio.h>
main()
{
int prime,i,b=0;
clrscr();
printf("\n Enter Number To Check Whether Prime or Not\n");
scanf("%d",&prime);

for(i=2;i<prime;i++)
{
if(prime%i==0)
{
b=1;
break;
}
}
if(b==0)
{
printf("\n Given Number is Prime");
}
else
{
printf("\n Given Number is Not Prime");
}
getch();
}

Pointer and Arrays in C++

 /* Pointer and Arrays */

#include<iostream.h>
#include<conio.h>
main()
{
int a[5],*p,i;
clrscr();

p=a;

*(a+0)=100;
a[1]=200;
a[2]=300;
a[3]=400;
a[4]=500;

p[0]=1000;
*(p+1)=2000;

for(i=0;i<5;i++)
cout<<"\n Value of Array :"<<a[i];

for(i=0;i<5;i++)
cout<<"\n Value of Pointer :"<<*(p+i);

getch();
}

Palindrome Program in C++

#include<iostream.h>
#include<conio.h>
#include<string.h>
main()
{
char str[50],str1[50];
int i,j;
clrscr();
cout<<"\n Enter A Length Of The String\n";
cin>>str;
for(i=0;str[i]!='\0';i++);

cout<<"\n The Length Of The Given String is \n"<<i;

for(j=0,i--;i>=0;j++,i--)
str1[j]=str[i];

str1[j]='\0';

cout<<"\n The Reverse Of Given String is \n"<<str1;
if(strcmp(str1,str)==0)
{
cout<<"\n Given String is Palindrome";
}
else
{
cout<<"\n Given String is not Palindrome";
}
getch();
}

Pointer Arithmetic Using C++

#include<iostream.h>
#include<conio.h>
main()
{
int a=100,*p;
clrscr();
p=&a;
cout<<"\n Pointer Arithmetic Result is :"<<(++*p);
getch();
}

Defining Member Functions Outside of The Class in C++

/*
Defining Member Functions Outside of The Class
==============================================

returntype classname::functionname(arg1,....,)
{
}

*/

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

class Rectangle
{
private:
int width,height;
public:
void Create(int,int);
void Display();
};
void Rectangle::Create(int w,int h) /* :: - Scope Resolution Operator */
{
width=w;
height=h;
}
void Rectangle::Display()
{
cout<<"\n Width :"<<width;
cout<<"\n Height :"<<height;
}
void main()
{
Rectangle r1,r2;
r1.Create(100,100);
r1.Display();
r2.Create(200,200);
r2.Display();
}

Polymorphism in C++

Polymorphism
============

Polymorphism means how to put the same thing can be used
for different purposes.

Polymorphism Can be Achieved in C++ By Two Ways
===============================================

1) Function OverLoading
2) Operator OverLoading

Operator OverLoading
====================

int a=10,b=20,c=0;
c=a+b;

float a1=10.4,b1=20.4,c1=0.0;
c1=a1+b1;


Function OverLoading
=====================

#include<iostream.h>
#inclue<conio.h>
main()
{
clrscr();
cout<<"\n Addition Of Two Integer is :"<<add(12,34);
cout<<"\n Addition Of Two real Number is :"<<add(12.5,34.4);
getch();
}
int add(int a,int b)
{
return a+b;
}
double add(double a,double b)
{
return a+b;
}

Constructor Order of Execution in C++

/* Order Of Execution  */

#include<iostream.h>
#include<conio.h>
class Base
{
public:
Base()
{
cout<<"\n I am Base Class Constructor";
}
~Base()
{
cout<<"\n I am Base Class Destructor";
}
};
class Derived:public Base
{
public:
Derived()
{
cout<<"\n I am Derived Class Constructor";
}
~Derived()
{
cout<<"\n I am Derived Class Destructor";
}
};
class Derived1:public Derived
{
public:
Derived1()
{
cout<<"\n I am Derived1 Class Constructor";
}
~Derived1()
{
cout<<"\n I am Derived1 Class Destructor";
}
};
void main()
{
clrscr();
Derived1 d;
getch();
}

Nested If Condition in C++

#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"\n Enter Number ( 1 - 2 - 3 ) :";
cin>>a;

if(a>0&&a<4)
{
if(a==1)
{
cout<<"\n Given Number is One";
}
else if(a==2)
{
cout<<"\n Given Number is Two";
}
else
{
cout<<"\n Given Number is Three";
}
}
else
{
cout<<"\n Wrong Number";
}
getch();
}

Operators in C++

Operators
=========
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Increment and Decrement Operator
5) Conditioal Operator
6) Assignment Operator
7) Bitwise Operator
8) Shortand Assignment Operator


Two Dimensional Array in C++

#include<iostream.h>
#include<conio.h>
main()
{
int i=0,j=0,a[3][3],b[3][3],c[3][3];
clrscr();
cout<<"\n Enter First Two Dimentional Array values \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
}

cout<<"\n Enter Second Two Dimentional Array values \n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>b[i][j];
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}


cout<<"\n First+Second Array\n";

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
getch();
}

Nested For Loop in C++

/* Nested For Loop */

#include<iostream.h>
#include<conio.h>
int main()
{
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"\t"<<i<<","<<j;
}
cout<<"\n";
}
return 0;
getch();
}

Inheritenc Sample Program in C++

/*
Syntax For Inheritence
======================
class DerivedClassName:accessspecifier BaseClassName
{

}
*/
#include<iostream.h>
#include<conio.h>

class Base
{
public:
void BaseClass()
{
cout<<"\n I am Base Class";
}
};
class Derived:public Base
{
public:
void BaseClass()
{
cout<<"\n I am Derived Class";
}
};
main()
{
Derived d;
clrscr();
d.BaseClass();
d.Base::BaseClass();
getch();
}

MultiLevel Inheritence Program in C++

/* MultiLevel Inheritence*/

#include<iostream.h>
#include<conio.h>
class Base
{
public:
void BaseClass()
{
cout<<"\n I am Base Class";
}
};
class Base1:public Base
{
public:
void BaseClass1()
{
cout<<"\n I am Base Class1";
}
};
class Derived:public Base1
{
public:
void DerivedClass()
{
cout<<"\n I am Derived Class";
}
};
main()
{
Derived d;
clrscr();
d.BaseClass();
d.BaseClass1();
d.DerivedClass();
getch();
}

Multiple Inheritence in C++

/* Multiple Inheritence */

#include<iostream.h>
#include<conio.h>
class Base
{
public:
void BaseClass()
{
cout<<"\n I am Base Class";
}
};
class Base1
{
public:
void BaseClass1()
{
cout<<"\n I am Base Class1";
}
};
class Derived:public Base,public Base1
{
public:
void DerivedClass()
{
cout<<"\n I am Derived Class";
}
};
main()
{
Derived d;
clrscr();
d.BaseClass();
d.BaseClass1();
d.DerivedClass();
getch();
}

Local variable in C++

#include<iostream.h>
#include<conio.h>
main()
{
{
auto int a=1;
cout<<"\n A Value is :"<<a;
}

{
int a=10;
cout<<"\n A Value is :"<<a;
}

{
int a=100;
cout<<"\n A Value is :"<<a;
}
getch();
}

Logical Operators in C++

/*
Logical Operators
==================

&&   (  AND  )
===

A B Result
======================
1 1 1
1 0 0
0 1 0
0 0 0


|| ( OR )
===
A B Result
======================
1 1 1
1 0 1
0 1 1
0 0 0


! ( NOT )
===
A Result
===============
1 0
0 1


*/
#include<iostream.h>
#include<conio.h>
main()
{
int a=10,b=5;
clrscr();
cout<<"\n a>15 && a<5 :"<<(a>15&&a<5);
cout<<"\n a>5  &&  a<5 :"<<(a>5&&a<5);
cout<<"\n a>15 && a<15 :"<<(a>15&&a<15);
cout<<"\n a>5  && a<15 :"<<(a>5&&a<15);

cout<<"\n a>15 || a<5 :"<<(a>15||a<5);
cout<<"\n a>5  || a<5 :"<<(a>5||a<5);
cout<<"\n a>15 || a<15 :"<<(a>15||a<15);
cout<<"\n a>5  || a<15 :"<<(a>5||a<15);

cout<<"\n !(a>b) :"<<!(a>b);
cout<<"\n !(a<b) :"<<!(a<b);
getch();
}


Manipulators in C++

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
main()
{
char ch;
clrscr();
while(!cin.eof())
{
cin>>resetiosflags(ios::skipws);
cin>>ch;
cout<<ch;
}
getch();
}

Increment and Decrement Operator In C++

/*
Increment and Decrement Operator
===============================
++ - Increment Operator
-- - Decrement Operator

++a - Pre Increment
a++ - Post Increment

--a - Pre Decrement
a-- - Post Decrement

*/

#include<iostream.h>
#include<conio.h>
main()
{
int a=10,b;
clrscr();

b=a++;

cout<<"\n B value is "<<b;
cout<<"\n A value is "<<a;

b=++a;

cout<<"\n B value is "<<b;
cout<<"\n A value is "<<a;

b=a--;

cout<<"\n B value is "<<b;
cout<<"\n A value is "<<a;

b=--a;

cout<<"\n B value is "<<b;
cout<<"\n A value is "<<a;

getch();
}

InHeritence Notes


InHeritence
============
Inheritence is the concept by which properties of
one entity are avilabel to another.

Using this concept you can create new classes from
existing classes instead of rewritten.

new class is known as Derived Class
Old Class is Known as Base Class

New Class inherits all the properties of base class
and add new properies here.
Old class is unchanged.


Types of inheritence
=====================

Single Inheritence
Multiple Inheritence
Multilevel Inheritence

Single Inheritence
==================
The Derived class have only one base class.

Multiple Inheritence
=====================
The New Class is derived from more than one base class.

Multilevel Inheritence
=======================
Level by Level of inherience is known as Multilevel.

Inline Function in C++

/*
Inline Function
===============

If You Make The Function as Inline then execution speed of
the function becomes higher compare to normal function.

If You Like To  Make a Function as Inline then you must not
include for loop,while loop,switch case and recursive
inside a function.

if you put any for or while inside a function,compiler
ignore inline and consider as a normal function.

defaultly all functions inside a class consider as a
inline function
*/

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

class Rectangle
{
private:
int width,height;
public:
void Create(int,int);
void Display();
};
inline void Rectangle::Create(int w,int h)
{
width=w;
height=h;
}
inline void Rectangle::Display()
{
cout<<"\n Width :"<<width;
cout<<"\n Height :"<<height;
}
void main()
{
Rectangle r1,r2;
r1.Create(100,100);
r1.Display();
r2.Create(200,200);
r2.Display();
}

Conditional Statements in C++

#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"\n Enter Number :";
cin>>a;

if(a>0)
{
cout<<"\n Given Number is Positive";
}
if(a<0)
{
cout<<"\n Given Number is Negative";
}
getch();
}

=====================

#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"\n Enter Number :";
cin>>a;

if(a>0)
{
cout<<"\n Given Number is Positive";
}
else
{
cout<<"\n Given Number is Negative";
}
getch();
}


Function in C++

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

main()
{
clrscr();

void printline(char); // Function Declaration

printline('*');
cout<<"\n\t\t Languages\n";
printline('&');

cout<<"\n\t\t C";
cout<<"\n\t\t C++";
cout<<"\n\t\t Java\n";

printline('-');

getch();
}

void printline(char c)
{
int i;
for(i=0;i<65;i++)
cout<<c;
}



====================


/* Passing Arrays to functions */

#include<iostream.h>
#include<conio.h>
int sum(int[],int);
main()
{
int a[5];
clrscr();

a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;

cout<<"\n Sum is :"<<sum(a,5);
getch();
}
sum(int a[],int n)
{
int sum=0,i;
for(i=0;i<n;i++)
sum=sum+a[i];
return sum;
}

===========================

#include<iostream.h>
#include<conio.h>
long PrimeCheck(long);
main()
{
long prime;
clrscr();
cout<<"\n Enter Number To Check Whether Prime or Not\n";
cin>>prime;
if(PrimeCheck(prime))
cout<<"\n Given No is not prime no";
else
cout<<"\n Given No is prime no";

getch();
}
long PrimeCheck(long prime)
{
long i;
for(i=2;i<prime;i++)
{
if(prime%i==0)
{
return 1;
}
}
return 0;
}

Goto Keyword in C++

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
goto Three;
One:
cout<<"\n I am First ";
goto Two;
Two:
cout<<"\n I am Second ";
goto End;
Three:
cout<<"\n I am Third ";
goto One;
End:
getch();
}

Friend Function in C++

/*  Friend Function  */

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

class Base2;

class Base1
{
private:
int a;
public:
void set(int b)
{
a=b;
}
       friend int add(Base1,Base2);
};
class Base2
{
private:
int b;
public:
void set(int c)
{
b=c;
}
      friend int add(Base1,Base2);
};

void main()
{
Base1 B1;
Base2 B2;
B1.set(100);
B2.set(200);
cout<<"\n Addition is :"<<add(B1,B2);
}
int add(Base1 B1,Base2 B2)
{
return B1.a+B2.b;
}

New and Delete Operator in C++

/*

Dynamic Memory Allocation
===========================

new - New Memory Allocation
delete - Memory Deallocation

*/

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

class Date
{
private:
int dd,mm,yy;
public:
void setDate(int a,int b,int c)
{
dd=a;
mm=b;
yy=c;
}
void Display()
{
cout<<"\n ToDay Date is\n";
cout<<dd<<"\\"<<mm<<"\\"<<yy;
}
};
void main()
{
clrscr();

Date *d=new Date;
d->setDate(21,2,2005);  // ->  - Member Access Operator
d->Display();
delete d;

Date *d1=new Date;
d1->setDate(22,2,2005);
d1->Display();
delete d1;

getch();
}

ElseIfLadder Program in C++

#include<iostream.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
cout<<"\n Enter Number1 :";
cin>>a;
cout<<"\n Enter Number2 :";
cin>>b;
cout<<"\n Enter Number3 :";
cin>>c;

if(a>b&&a>c)
{
cout<<"\n First Number is Big";
}
else if(b>a && b>c)
{
cout<<"\n Second Number is Big";
}
else
{
cout<<"\n Third Number is Big";
}
getch();
}

ForLoop Demo in C++

#include<iostream.h>
#include<conio.h>
main()
{
int i;
clrscr();
for(i=0;i<10;i++)
{
cout<<"\nSSI Education";
}
getch();
}

Default Argument Constructor in C++

/*
Default Argument Constructor & Destructor
==========================================
*/

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

class Date
{
private:
int dd,mm,yy;
public:
Date(int d=10,int m=2,int y=2005) /* Default Argument Constructor */
{
dd=d;
mm=m;
yy=y;
}
void Display()
{
cout<<"\n ToDay Date is\n";
cout<<dd<<"\\"<<mm<<"\\"<<yy;
}
~Date()
{
cout<<"\n Destructor Called";
}
};
void main()
{
clrscr();
Date d;    /* Default Constructor is Called */
d.Display();
Date d1(22,5);  /* Constructor is Called */
d1.Display();
Date d2(22,4,2006);
d2.Display();
getch();
}

DoWhile Loop in C++

#include<iostream.h>
#include<conio.h>
main()
{
int i=0;
clrscr();

do
{
cout<<"\n"<<i;
i++;
}while(i<20);
i=0;

do
{
cout<<"\n"<<i;
i+=3;
}while(i<20);
getch();
}

Continue Keyword in C++

#include<iostream.h>
#include<conio.h>
main()
{
int i=0;
clrscr();

for(i=0;i<100;i++)
{
if(i<60)
continue;
cout<<"\n"<<i<<".. SSI Education";
}

getch();
}

Copy Constructor in C++

/*
Copy constructor & Destructor
============================
*/


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

class Date
{
private:
int dd,mm,yy;
public:
Date()
{
dd=0;
mm=0;
yy=0;
}
Date(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
Date(Date *p)  /* Copy Construtor */
{
dd=p->dd;
mm=p->mm;
yy=p->yy;
}
void Display()
{
cout<<"\n ToDay Date is\n";
cout<<dd<<"\\"<<mm<<"\\"<<yy;
}
~Date()
{
cout<<"\n Destructor Called";
}
};
void main()
{
clrscr();
Date d1(22,2,2005);
Date d;
d.Display();
d=d1;
d.Display();
d1.Display();
getch();
}

Destructor in C++

/*
Copy constructor & Destructor
============================
*/


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

class Date
{
private:
int dd,mm,yy;
public:
Date()
{
dd=0;
mm=0;
yy=0;
}
Date(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
Date(Date *p)  /* Copy Construtor */
{
dd=p->dd;
mm=p->mm;
yy=p->yy;
}
void Display()
{
cout<<"\n ToDay Date is\n";
cout<<dd<<"\\"<<mm<<"\\"<<yy;
}
~Date()
{
cout<<"\n Destructor Called";
}
};
void main()
{
clrscr();
Date d1(22,2,2005);
Date d;
d.Display();
d=d1;
d.Display();
d1.Display();
getch();
}

Constructor OverLoading in C++

/*
Constructor and Constructor OverLoading
=======================================
*/
#include<iostream.h>
#include<conio.h>

class Date
{
private:
int dd,mm,yy;
public:
Date()  /* Default Constructor */
{
dd=11;
mm=2;
yy=2005;
}
Date(int d,int m,int y)     /* Three Argument Constructor */
{
dd=d;
mm=m;
yy=y;
}
void Display()
{
cout<<"\n ToDay Date is\n";
cout<<dd<<"\\"<<mm<<"\\"<<yy;
}
};
void main()
{
clrscr();
Date d; /* Default Constructor is Called Automatically*/
d.Display();

Date d1(22,2,2005); /* Three Argument Constructor is Called */
d1.Display();
getch();
}

Definition of Constructor in C++

Constructors
==============

A Constructor is a special member function for initialize a object.

Rules
=====

1) Constructor name should be same as classname
2) Constructor Should Not have any return type & should not return value.
3) Constructor shold also be overloaded
4) Constructor function Should not be called explicitly

Notes
=====
A Constructor is automatically called whenever creating object.

Destructor
==============
A Destructor is a special member function for deinitialize a object.

Rules
=====
1) Destructor name and Class Name should be same.
2) Destructor Should Not have any return type & should not return any value.
3) Destructor should not be overloaded.
4) Destructor Should not be called explicitly.
5) Destructor is differenciated from Constructor by tilde(~) Symbol.

Notes
=====
A Destructor Function is automatically called
whenever all duties of the object is over for erase a object from memory.

Classes and Objects Program in C++


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

class Circle
{
private:
int radious;
public:
void Create(int w)
{
radious=w;
}
float Area()
{
return 3.14*radious*radious;
}
};
void main()
{
Circle C1;
clrscr();
C1.Create(100);
cout<<"\n Area of the circle is "<<C1.Area();
getch();
}

Conditional Operator in C++

/*

Conditional Operator or Ternary Operator  ( ?: )
=========================================

Syntax:
=======

Expr1 ? Expr2 : Expr3;

If Expr1 = True Then Execute Expr2
If Expr1 = False Then Execute Expr3

*/
#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"\n Enter Number :";
cin>>a;
cout<<"\n Given Number is :"<<(a>0?"Positive":"Negative");
cout<<"\n Given Number is :"<<(a%2==0?"Even":"Odd");
getch();
}

Handling Break Keyword in C++

#include<iostream.h>
#include<conio.h>
main()
{
int i=0,Br;
clrscr();
cout<<"\n Enter Value To Break The Loop";
cin>>Br;
for(i=0;i<100;i++)
{
cout<<"\n"<<i<<".. SSI Education";
if(i==Br)
{
break;
}
}
getch();
}

Call by Reference in C++

#include<iostream.h>
#include<conio.h>
void swap(int*,int*);
main()
{
int a=10,b=20;
clrscr();
swap(&a,&b);
cout<<"\n A Value is :"<<a;
cout<<"\n B Value is :"<<b;
getch();
}
void swap(int *x,int *y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
}

Call by Value in C++

#include<iostream.h>
#include<conio.h>
void swap(int,int);
main()
{
int a=10,b=20;
clrscr();
swap(a,b);
}
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
cout<<"\n A Value is :"<<a;
cout<<"\n B Value is :"<<b;
}

Finding Biggest and Smallest In List of Numbers Using C++

#include<iostream.h>
#include<conio.h>
main()
{
int a[100],n,i,j=0;
clrscr();
cout<<"\n How Many Numbers You Want Give Input\n";
cin>>n;
cout<<"\n Enter Your Numbers\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
i=0;
while(i<n)
{
if(a[i]>j)
{
j=a[i];
}
i++;
}
cout<<"\n Biggest No is :"<<j;
i=0;
j=a[i];
i++;
while(i<n)
{
if(a[i]<j)
{
j=a[i];
}
i++;
}
cout<<"\n Smallest No is   :"<<j;
getch();
}

Binary File Handling in C++

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
main()
{
clrscr();

ofstream out("Items.txt",ios::out|ios::binary);

for(int i=65;i<91;i++)
out.put(char(i));
out.close();

ifstream in("Items.txt",ios::in|ios::binary);
char ch;
while(in)
{
in.get(ch);
cout<<ch;
}
out.close();
getch();
}

Base Class Initialization Program Using C++

// Base Class Initialization

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

class Base
{
protected:
int a,b;
public:
Base()
{
a=0;
b=0;
}
Base(int A,int B)
{
a=A;
b=B;
}
};
class Derived:public Base
{
private:
int c;
public:
Derived()// automatically call Base Class Constructor
{
c=0;
}
Derived(int A,int B,int C):Base(A,B)
{
c=C;
}
void Put()
{
cout<<"\n A =  "<<a;
cout<<"\n B =  "<<b;
cout<<"\n C =  "<<c;
}
};
void main()
{
Derived d,d1(10,20,30);
clrscr();
d.Put();

d1.Put();
getch();
}

Ascending Order Program Using C++

#include<iostream.h>
#include<conio.h>
main()
{
int a[100],n,i,j,sum=0;
clrscr();
cout<<"\n How Many Numbers You Want Give Input\n";
cin>>n;
cout<<"\n Enter Your Numbers\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

cout<<"\n Sorted Numbers\n";
for(i=0;i<n;i++)
{
cout<<"\n"<<a[i];
}
getch();
}

Arithmetic Operator Program Using C++

/*
Arithmetic Operators
====================
+
-
*
/

% -     Modulo Operator  ( Reminder After Division )
*/

#include<iostream.h>
#include<conio.h>
main()
{
int a=10,b=5;
clrscr();
cout<<"\n A   +   B   ="<<(a+b);
cout<<"\n A   -   B   ="<<(a-b);
cout<<"\n A   *   B   ="<<(a*b);
cout<<"\n A   /   B   ="<<(a/b);
cout<<"\n A   %   B   ="<<(a%b);
getch();
}

Addition of Two Numbers using C++

/*
Sample Program For Addition Of Two Numbers
==========================================
*/

#include<iostream.h>
#include<conio.h>
main()
{
int a;
clrscr();
cout<<"\n Enter Number1 :";
cin>>a;

int b;
cout<<"\n Enter Number2 :";
cin>>b;

cout<<"\n Addition is "<<(a+b);
getch();
}

Graphics Programming in C++

/* rectangle, outtextxy, setbkcolor */

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

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,” “);
clrscr();
line(200,0,0,200);
line(300,100,100,300);
line(100,300,0,200);
line(300,100,200,0);
outtextxy(125,150,”AYYAPPAN”);
setbkcolor(13);
getch();
closegraph();
}

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,n;
initgraph(&gd,&gm,” “);
clrscr();
n=0;
while(n<=15)
{
outtextxy(200,200,”Enter a number(0-15)”);
cin>>n;
setbkcolor(n);
}
getch();
}

/*settextstyle(int font,int direction, int size);

font = 0 to 11
direction = 0 or 1; HORIZ_DIR or VERT */
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,” “);
clrscr();
settextstyle(4,0,5);
outtextxy(200,200,”AYYAPPAN”);
getch();
}

/* setfillstyle(style,color);
Styles    {“EMPTY_FILL”,
                     “SOLID_FILL”,
                     “LINE_FILL”,
                     “LTSLASH_FILL”,
                     “SLASH_FILL”,
                     “BKSLASH_FILL”,
                     “LTBKSLASH_FILL”,
                     “HATCH_FILL”,
                     “XHATCH_FILL”,
                     “INTERLEAVE_FILL”,
                     “WIDE_DOT_FILL”,
                     “CLOSE_DOT_FILL”,
                     “USER_FILL”}; */

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,” “);
clrscr();
arc(200,200,0,90,50);
setfillstyle(6,5);
pieslice(200,200,0,180,50);
                             360,100     
getch();
}

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

void main()
{
int gd=DETECT,gm,n,x;
char ch;
initgraph(&gd,&gm,” “);
clrscr();
n=0;
x=30;
while(ch!=’x’)
{
if(n>=15)
n=0;
setcolor(n);
circle(270,220,x);
ch=getch();
n=n+1;
x=x+10;   (or)     x=x-5;
}
getch();
}

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
int main(void)
{
int gdriver=DETECT,gmode,errorcode;
int midx,midy,i;
initgraph(&gdriver,&gmode,” “);
clrscr();
bar3d(200,200,100,100,10,1);
getch();
}

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x1=100,x2=200,y1=200,y2=200;
initgraph(&gd,&gm,” “);
clrscr();
line(x1,y1,x2,y2);
line(x1,y1,x1+50,y1-100);
line(200,200,150,100);
getch();

}