Você está na página 1de 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

FUNCTION OVERLOADING:
#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(); }

OUTPUT:

Enter two integers 4 9 sum of integers:13 Enter two floating point numbers 4.5 8.2 sum of floats:12.7

ERROR HANDLING DURING FIVE OPERATIONS:


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

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

OUTPUT: Enter the two number and its operator: 30 + 30 30 + 30 = 60 Enter the two number and its operator: 30 - 30 30 - 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

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

class Crectangle: public Cpolygon { public: int area () { return (width * height); }

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

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

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

OUTPUT:
Area=(0.5*base*height) Where,base=4,height=6 Area=12

MANIPULATORS:
#include <iostream.h> #include <stdlib.h> #include <iomanip.h> #include<conio.h> void main ( ) { float x = 23.012345678; float y = 44; float z = 3.765; float a = 4.5; clrscr(); cout << 2345.012345678 << endl; cout << 0.005544332211 << endl; cout << x << endl; cout << y << endl; cout << z << endl; cout << a << endl; cout << x << y << z << a << endl; system("PAUSE"); getch(); }

OUTPUT:

2345.012346 0.005544 23.012346 44 3.765 4.5 23.012346443.7654.5

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

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

OUTPUT:
Enter the 16 intergers for data. 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

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

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

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

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

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

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

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

PURE VIRTUAL FUNCTION:


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

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

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

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

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

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

CLASSES AND OBJECTS:


#include<iostream.h> #include<conio.h> class Box { public: double length; double breath; double height; }; int main() { clrscr(); Box Box1; Box Box2; double volume=0.0; Box1.height=5.0; Box1.length=6.0; Box1.breath=7.0; Box2.height=10.0; Box2.length=12.0; Box2.breath=13.0;

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

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

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

return 0; }

OUTPUT:
x=20

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

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

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

USER DEFINE FUNCTION TO COMPARE TWO STRINGS:


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

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

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

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

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

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

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

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

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

OUTPUT:
Hello world! Good bye world!

UNARY AND BINARY OPERATORS:


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

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

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

RELATIONAL, LOGICAL AND BITWISE OPERATORS:


#include <iostream.h> #include <conio.h> int main() { unsigned short flags1; unsigned short flags2; flags1 = 0x00FF; flags2 = 0xFF00; clrscr(); cout << "Bitwise Operator: " << (flags1 & flags2) << endl; cout << "Logical Operator: " <<(flags1 && flags2) << endl; getch();

return 0; }

OUTPUT:
Bitwise Operator: 0 Logical Operator: 1

Você também pode gostar