You are on page 1of 56

# FRIEND FUNCTION

:
#include<iostream.h> #include<conio.h> class ONE; class TWO { public: void print(ONE&x); }; class ONE { int a,b; friend void TWO::print(ONE&x); public: ONE():a(1),b(2) { } }; void TWO::print(ONE&x) { cout<<"a is"<<x.a<<endl; cout<<"b is"<<x.b<<endl;

} int main() { clrscr(); ONE xobj; TWO yobj; yobj.print(xobj); return 0; getch(); }

OUTPUT:

a is 1 b is 2

#include<iostream.h> #include<conio.h> long add(long,long); float add(float,float); int main() { long a,b,x; float c,d,y; clrscr(); cout<<"Enter two integers\n"; cin>>a>>b; x=add(a,b); cout<<"sum of integers:"<<x<<endl; cout<<"Enter two floating point numbers\n"; cin>>c>>d; y=add(c,d); cout<<"sum of floats:"<<y<<endl; return 0; } long add (long x,long y) {

long sum; sum=x+y; return sum; } float add(float x,float y) { float sum; sum=x+y; return sum; getch(); }

7 .5 8.2 sum of floats:12.OUTPUT: Enter two integers 4 9 sum of integers:13 Enter two floating point numbers 4.

break. break.h> int main() { double Operand1. case '-': Result = Operand1 . cin >> Operand1 >> Operator >> Operand2.h> #include<conio. switch(Operator) { case '+': Result = Operand1 + Operand2.Operand2. case '/': . char Operator. break. Result.ERROR HANDLING DURING FIVE OPERATIONS: #include<iostream. Operand2. case '*': Result = Operand1 * Operand2. cout << "\n Enter the two number and its operator:". clrscr().

break. default: cout << "Bad Operation". cout << "\n\n". return 0. } . } cout << "\n" << Operand1 << " " << Operator << " "<< Operand2 << " = " << Result. getch().Result = Operand1 / Operand2.

30 = 0 Enter the two number and its operator: 30 * 30 30 * 30 = 900 Enter the two number and its operator: 30 / 30 30 / 30 = 1 .30 30 .OUTPUT: Enter the two number and its operator: 30 + 30 30 + 30 = 60 Enter the two number and its operator: 30 .

h> #include <iostream. class Crectangle: public Cpolygon { public: int area () { return (width * height).FORMS OF INHERITANCE: #include<conio. height. height=two. int two) { width=one. } }. } .h> class Cpolygon { protected: int width. public: void input_values (int one.

area()<<endl.2). return 0. clrscr().input_values (2. rectangle. getch(). cout<<"\n area of rectangle:"<<rectangle.area()<<endl. cout<<"\n area of triangle:"<<triangle. } }.input_values (2.}. } . class Ctriangle: public Cpolygon { public: int area () { return (width * height / 2). Ctriangle triangle. triangle. int main () { Crectangle rectangle.2).

OUTPUT: area of rectangle:4 area of triangle:2 .

a=triangle_area(b.a. getch().base=4.h). } int main(void) { float b.h> #include<conio. clrscr().float height) { float area.height=6"<<endl. cout<<"Where.5*base*height)"<<endl. } .5*base*height). cout<<"\n Area="<<a<<endl.h> inline float triangle_area(float base. area=(0.h.INLINE FUNCTION: #include<iostream. return area. cout<<"Area=(0. return 0. h=6. b=4.

base=4.5*base*height) Where.height=6 Area=12 .OUTPUT: Area=(0.

float a = 4. clrscr(). cout << a << endl. system("PAUSE"). cout << y << endl. cout << 2345.012345678 << endl. } . getch().h> #include <iomanip.5.h> #include <stdlib. cout << 0. cout << z << endl.005544332211 << endl.012345678. float y = 44. cout << x << endl.MANIPULATORS: #include <iostream. cout << x << y << z << a << endl.h> #include<conio. float z = 3.765.h> void main ( ) { float x = 23.

5 .012346 0.5 23.012346443.OUTPUT: 2345.005544 23.7654.765 4.012346 44 3.

h> int main() { const int Rows=4.c++) matrix[Rows][Columns]=0.r<Rows.c++) for(r=0. int matrix[Rows][Columns]. for(r=0. for(c=0. const int Columns=4. int c. clrscr(). const int End_of_row=4.r<Rows.r++) .c<Columns.h> #include<iomanip. int copyarray.h> #include<conio. cout<<"Enter the 16 intergers for data.r++) matrix[Rows][Columns]=0.MULTI-DIMENSIONAL ARRAYS: #include<iostream. for(c=0.c<Columns. int r. const int End_of_col=4."<<endl.

r<Rows.c<Columns. for(r=0.c++) cout<<setw(5)<<matrix[r][c]<<"".r++) .} cout<<endl. for(r=0. } return 0.c++) { for(r=0.r++) { for(c=0. getch().cin>>matrix[r][c].r++) { for(c=0.c++) cout<<setw(3)<<matrix[c][r]<<"".r<Rows. cout<<endl. cout<<endl. } .} for(c=0.c<Columns. .c<Columns.r<Rows.

1234567812345678 1 5 1 5 2 6 2 6 3 7 3 7 4 8 4 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 .OUTPUT: Enter the 16 intergers for data.

} distance(int ft. inches=0.SPECIAL OPERATORS: #include<iostream. } void getdist() { cout<<"\n Enter feet:". . cin>>feet. inches=in.h> class distance { int feet. float inches.0. public: distance() { feet=0.float in) { feet=ft.h> #include<conio.

}. distance distance :: operator+(distance d2) { int f=feet+d2.inches. if(i>=12.feet.0) { i=i-12. cin>>inches. f++.cout<<"\n Enter inches:". } void main() { . } distance operator+(distance). } return distance(f.i).0. float i=inches+d2. } void showdist() { cout<<feet<<"\'-"<<inches<<'\"'.

25).showdist(). cout<<"\n dist3=". dist4=dist1+dist2+dist3. dist1. dist2. dist4. cout<<"\n dist2=". } . distance dist1. dist3=dist1+dist2. dist1.showdist().clrscr(). getch().getdist(). dist3.showdist().dist4. cout<<"\n dist4=".6. cout<<"\n dist1=".dist2(11.showdist().dist3.

5 dist1=7'-13.5" dist2=11'-6.25" dist3=19'-7.75" dist4=39'-3.5" .OUTPUT: Enter feet:7 Enter inches:13.

numbers::numbers(char nm[].0).STREAM CLASSES: #include<iostream. a=j.nm).h> class numbers { public: numbers(char[]="unknown". b=k.float k) { strcpy(name. }.int=0. void display().int j. void update().h> #include<string.float=0. private: char name[30]. float b. int a.h> #include<conio. } void numbers::update() .

} void numbers::display() { cout<<"\n The name is"<<name<<"\n".no2("John". no2.display(). cin>>a>>b.678.9).12345. getch(). } . } void main() { clrscr(). cout<<"\n The number are"<<a<<"and"<<b<<endl. numbers no1. no1.{ cout<<"Enter a and b"<<endl.display().

OUTPUT: The name is unknown The number are 0 and 0 The name is John The number are 12345 and 678.900024 .

h> #include<conio. } virtual voidvirtual() { cout << "Base virtual called.PURE VIRTUAL FUNCTION: #include <iostream. } }.\n".\n".h> class Base { public: void Nonvirtual() { cout << "Base Nonvirtual called.\n". } . class Derived : public Base { public: void Nonvirtual() { cout << "Derived Nonvirtual called.

getch().voidvirtual() { cout << "Derived virtual called. bBase->voidvirtual(). Base* bBase = new Base(). Base* bDerived = new Derived(). bBase->Nonvirtual(). bDerived->Nonvirtual(). } . } }.\n". bDerived->voidvirtual(). int main() { clrscr().

Base Nonvirtual called. Base virtual called. Derived virtual called. .OUTPUT: Base Nonvirtual called.

cout << endl << endl. void selectionSort(int arr[]. return 0. for (k = 0. for (k = 0. k < MAXSIZE. k < MAXSIZE. . k++) cout << numbers[k] << " ". int& y).h> #include <stdlib. cout << "AFTER SORT: ". 3. int size). 5. 19. int main() { int numbers[] = { 13. selectionSort(numbers. 9. 1. 17. 15 }. MAXSIZE). cout << endl << endl << endl. void swap(int& x. system("PAUSE"). int k. cout << "BEFORE SORT: ". 7. k++) cout << numbers[k] << " ". 11.COMMAND LINE ARGUMENTS TO PRINT THE GIVEN NUMBER IN DESCENDING AND ASCENDING ORDER: #include <iostream.h> const int MAXSIZE = 10.

int size) { int indexOfMin. temp = x.1. arr[indexOfMin]).} void selectionSort(int arr[]. } . int& y) { int temp. x = y. j++) if (arr[j] < arr[indexOfMin]) indexOfMin = j. pass++) { indexOfMin = pass. pass < size . for (pass = 0. y = temp. for (j = pass + 1. pass. } } void swap(int& x. swap(arr[pass]. j. j < size.

OUTPUT: BEFORE SORT: 13 5 1 7 9 11 3 17 19 15 AFTER SORT: 1 3 5 7 9 11 13 15 17 19 .

0. Box Box1. Box1. double height. Box2. Box2.h> #include<conio. Box2.0. double volume=0.0.height=10.0.height=5. Box1.length=6.breath=13. }.CLASSES AND OBJECTS: #include<iostream. .length=12.breath=7.0. Box1.0.h> class Box { public: double length.0. double breath. Box Box2. int main() { clrscr().

return 0. getch().height*Box1.volume=Box1. cout<<"Volume of Box2:"<< volume<<endl. cout<<"Volume of Box1:"<<volume<<endl.height*Box2.length*Box2.breath. } . volume=Box2.length*Box1.breath.

OUTPUT: Volume of Box1:210 Volume of Box2:1560 .

int x=20.HIDE FUNCTION: #include<iostream.h> class test { private: int x. public: void setx(int x) { this->x=x.setx(x). } }. int main() { test obj.print(). . } void print() { cout<<"x="<<x<<endl. obj. obj.

} .return 0.

OUTPUT: x=20 .

else cout<<"FAIL\n". cout<<"\n\tHIGHEST VALUE CALCULATION\n".tot. clrscr().CONTROL STATEMENT: #include<iostream. cout<<"\tRESULT\n" .h> #include<conio. cout<<"\n\t-------------------------\n". if(a>=35&&b>=35) cout<<"PASS\n".h> void main() { int a. getch().b. cin>>a>>b. if(a!=b) { . cout<<"Enter the Tamil and English mark:\n". cout<<"\t------\n". if(a<0||b<0) cout<<"There is negative value\n\n".avg.

else if(avg>=60) cout<<"First Class". getch().AVERAGE CALCULATION\n". } else cout<<"Both are equal\n". else if(avg>=35) cout<<"Second Class". cout<<"Your Average is:"<<avg<<"\n". if(avg>=75) cout<<"Distinction". else cout<<"Your Average is below Second Grade:". cout<<"Your Total is :"<<tot<<"\n" .} . tot=a+b. cout<<"\n\tTOTAL.if(a>b) cout<<"Tamil mark is high\n". avg=tot/2. cout<<"\n\t-------------------------\n". else cout<<"English mark is high\n".

AVERAGE CALCULATION -----------------------------------------Your Total is :170 Your Average is:85 Distinction .OUTPUT: Enter the Tamil and English mark: 90 80 RESULT ---------PASS HIGHEST VALUE CALCULATION -----------------------------------------Tamil mark is high TOTAL.

h> #include<string. cin>>str. void oper1() { cout<<"\nEnter the FIRST string :".str1)==0) { cout<<"\nGiven string both are equal" . } void oper2() { if(strcmp(str. cin>>str1.USER DEFINE FUNCTION TO COMPARE TWO STRINGS: #include<iostream. cout<<"\nEnter the SECOND string :".str1[30].h> #include<conio. } .h> class kan { public: char str[30].

void main() { clrscr().oper1(). s.else { cout<<"\nGiven string both are not equal". getch(). kan s. } } }. s.oper2(). } .

OUTPUT: Enter the FIRST string : C++ Enter the SECOND string : C++ Given string both are equal Enter the FIRST string : C++ Enter the SECOND string : C Given string both are not equal .

public: void set(int input_class. int postage. int width. public: void set(int l. } int get_postage(void){ return postage.h> class mail_info { int shipper. mail_info label.NESTED CLASS: #include <iostream. int post) { . int ship. class box { int length. postage = input_postage.} }. int w. int input_postage) { shipper = input_class.

2. medium.get_area()<<endl<<endl. cout<<"Area of small box\'s label is "<<small.get_area()<<endl<<endl.6.set(5. small. label.35).set(2.98). large. width = w. cout<<"Area of medium box\'s label is "<<medium.72).set(ship. void main(void) { box small.length = l. return. cout<<"Area of large box\'s label is "<<large.10.} }. } int get_area(void) { return (length * width).1.4.set(8. post).get_area()<<endl<<endl. medium.4. large. } .

OUTPUT: Area of small box's label is 8 Area of medium box's label is 30 Area of large box's label is 80 .

}. class C : public A { public: C() {} void action() . } }. class B : public A { public: B() {} void action() { cout<<"\n Hello world!".h> class A { public: virtual void action() = 0.h> #include <conio.ABSTRACT CLASS: #include <iostream.

Aclass A. } private: A *Apointer. char** argv) { clrscr(). } void dosomething() { Apointer->action(). int main(int argc. . }. class Aclass { public: void setinst(A *A_inst) { Apointer = A_inst.{ cout<<"\n Good bye world!". } }.

A.dosomething(). } .dosomething(). A. C Cinst. A.B Binst.setinst(&Binst). return 0. getch(). A.setinst(&Cinst).

OUTPUT: Hello world! Good bye world! .

int C) { a = A.h> class Minus { private: int a. .UNARY AND BINARY OPERATORS: #include<iostream. cout<<"\n\t c= " << c << endl . b = B. c = C. void operator . public: Minus(int A. int B.( ). void Minus :: display(void) { cout<<"\n\t a= " << a << endl . }. cout<<"\n\t b= " << b << endl . b. c .h> #include<conio. } void display(void).

} .}inline void Minus :: operator . M.( ) { a = -a . M. 10. getch().( )" . Clrscr(). -M . cout << "\nBefore activating operator .display( ) . } int main(void) { Minus M(5. c = -c . cout << "\nAfter activating operator . b = -b .display( ) . -15) . return 0.( )" .

( ) a= 5 b= 10 c= -15 After activating operator .( ) a= -5 b= -10 c= 15 .OUTPUT: Before activating operator .

cout << "Bitwise Operator: " << (flags1 & flags2) << endl. flags1 = 0x00FF.h> #include <conio. } .RELATIONAL. cout << "Logical Operator: " <<(flags1 && flags2) << endl. getch().h> int main() { unsigned short flags1. flags2 = 0xFF00. LOGICAL AND BITWISE OPERATORS: #include <iostream. return 0. unsigned short flags2. clrscr().

OUTPUT: Bitwise Operator: 0 Logical Operator: 1 .