Sie sind auf Seite 1von 10

#include <iostream> using namespace std; void swap(int *x, int *y); int main(){ int i,j; i=10;

j=20; cout << "initial value of i and j is " <<i<<" and " <<j<<endl; swap( &j, &i); cout <<"swap value of i and j is " <<i<< " and " <<j<<endl; return 0; } void swap( int *x, int *y) { int temp; temp= *x; *x = *y; *y = temp; } *********************************************** #include <iostream> using namespace std; void swap(int *x, int *y); int main() { int total, val; int *ptr; total = 3200; ptr = &total;

val= *ptr; cout<< "Total is" << val << endl; return 0; } *********************************************** #include <iostream> using namespace std; void swap(int *x, int *y); int main() { int total, val,num; int *ptr; int *p; total = 3200; ptr = &total; val= *ptr; p = &num; *p =100; cout<< num<<endl; (*p)++; cout <<num <<endl; (*p)--; cout << num <<endl; cout<< "Total is" << val << endl; return 0; }

#include <iostream> using namespace std; void repchar(); int main() { cout << "Oasis of the seas" <<endl; repchar(); return 0; } void repchar () { for (int j=0; j<20; j++) cout << '*'; cout << endl; } *********************************************** #include <iostream> using namespace std; void repchar(char ch, int n); int main() { repchar('^',40); cout << "Oasis of the seas" <<endl; repchar('=',44); return 0; } void repchar (char ch, int n) { for (int j=0; j<n; j++) } { } }

cout << ch; cout << endl;

********************************************** #include <iostream> using namespace std; int fibonacci (int); int main() { int max = 23; for (int loop =0;loop<= max; ++loop) cout << "Fibonacci ("<<loop<<") = "<< fibonacci(loop)<<endl; cout <<endl; return 0;

int fibonacci (int n)

int fib [23]; fib[0]=0; fib[1]=1; for (int j=2; j<=n; ++j) fib[j]= fib [j-1]+fib [j-2];

return fib[n];

//class example #include<iostream> using namespace std; class cRectangle { int x,y; public: void set_values(int,int); int area() { return (x*y); } }; void cRectangle::set_values(int a, int b) { x=a; y=b; int main () { cRectangle rect ; rect.set_values(3,4); cout <<"Area =" << rect.area() ; return 0; } *********************************************** //class example #include<iostream> using namespace std; class cRectangle { int x,y; //by default private } //by default private

public: void set_values(int,int); int area() { return (x*y); } }; void cRectangle::set_values(int a, int b) { x=a; y=b; int main () { cRectangle recta,rectb ; recta.set_values(3,4); rectb.set_values(5,6); cout <<"Area of A =" << recta.area() <<endl; cout <<"Area of B =" << rectb.area() <<endl; return 0; } *********************************************** //class example #include<iostream> using namespace std; }

class cRectangle { int x,y; public:

//by default private

cRectangle(int , int) ;

int area() { return (x*y); } }; cRectangle::cRectangle(int a, int b) // constructor name is cRectangle { x=a; y=b; int main () { cRectangle recta(3,4); cRectangle rectb(5,6); cout <<"Area of A =" << recta.area() <<endl; cout <<"Area of B =" << rectb.area() <<endl; return 0; } } }; public:

int x,y;

cTriangle(int , int) ; int area() { return (0.5*x*y); }

cRectangle::cRectangle(int a, int b) // constructor name is cRectangle { x=a; y=b; }

cTriangle::cTriangle(int a, int b) // constructor name is cRectangle { x=a; y=b; int main () { cRectangle recta(3,4); cRectangle rectb(5,6); cout <<"Area of A =" << recta.area() <<endl; cout <<"Area of B =" << rectb.area() <<endl; }

*********************************************** //class example #include<iostream> using namespace std; class cRectangle { int x,y; public: cRectangle(int , int) ; int area() { return (x*y); }; class cTriangle { //by default private } //by default private

cTriangle rectd(3,4); cTriangle recte(5,6); cout <<"Area of D =" << rectd.area() <<endl; cout <<"Area of E =" << recte.area() <<endl; return 0; }

#include<iostream> using namespace std; class cPolygon{ prive class protected: int width, height; public: void set_values(int a, int b) { width =a; height=b; }; class cRectangle: public cPolygon //class derived_class_name { public: int area() { return (width*height);} }; class cTriangle: public cPolygon{ public: int area() { return(width*height/2);} }; int main(){ cRectangle rect; cTriangle trgl; } }; }; public: } //derive class cant access its

rect.set_values (4,5); trgl.set_values(4,5); cout << rect.area() <<endl; cout <<trgl.area() << endl; return 0; }

*********************************************** #include<iostream> using namespace std; class cPolygon{ //derive class cant access its prive class, class will be protected protected: int width, height;

void set_values(int a, int b) { width =a; height=b; }

class cOutput{ public: void output(int i);

void cOutput:: output(int i){ cout<< i << endl;

class cRectangle: public cPolygon, public cOutput //class derived_class_name { public: int area()

{ return (width*height);} };

CRectangle (int,int); int area () { return (x*y);

class cTriangle: public cPolygon, public cOutput{ public: int area() { return(width*height/2);} }; int main(){ cRectangle rect; cTriangle trgl; public: };

class CTriangle { int x,y;

CTriangle (int,int); int area () { return (x*y/2);

rect.set_values (4,5); }; trgl.set_values(4,5); rect.output(rect.area()); trgl.output(trgl.area());

CRectangle :: CRectangle () { x=5; y=5;

return 0;

} CRectangle :: CRectangle (int a,int b) { x=a; y=b; }

*********************************************** #include <iostream> using namespace std; class CRectangle { int x,y; public: CRectangle();

CTriangle :: CTriangle (int a,int b) { x=a; y=b; }

int main() { CRectangle recta(3,4); CRectangle rectb(5,6); CTriangle rectc(7,8); CRectangle rectd; cout<<"area="<<recta.area()<<endl; cout<<"area="<<rectb.area()<<endl; cout<<"area="<<rectc.area()<<endl; cout<<"area="<<rectd.area()<<endl; return 0; } ***************************** #include<iostream> using namespace std; void foo(int y) { cout << "y= " <<y << endl; y=6; cout <<"y = " << y <<endl; int main () { int x=5; cout << "x= " << x << endl; foo(x); cout<<"x= " <<x <<endl; return 0; } } // void foo(int &y) // & is for reference

*********************************************** //pointer related to address #include <iostream> using namespace std; int main() { int i=5, val=10; int *ptrval = &val; // int *ptrval ; ptrval = &val

int *otherptr; // to define point * is given (*ptrval)++; cout << *ptrval++ <<endl;

ptrval = &i;

// address

is written this format

cout << *ptrval <<endl; (*ptrval)++; cout << *ptrval++ <<endl;

return 0; } ******************************* //pointer related to address #include <iostream> using namespace std; int main() { int b[100]; //b is an array of 100 integers

int *p;

int i=5;int val=10; int &refval=val;

p=&b[0]; p=p+1;

refval++; cout<<"refval++ ="<<refval++<<" val = "<<val<<endl; refval=i;

*p = 22; int *ip; int a[10]; ip= &a[3] // a0,a1,a2,a3 ... *(ip+3) =7 ; *(ip-2) =10; return 0; } **************************************** { int a[100]; int sum=0; for (int *p = a; p<a+100; p++) { sum += *p ; } cout << sum << endl; return 0; } }

refval++; cout<<"refval = "<<refval<<" i = "<<i<<endl; return 0;

******************** #include <iostream> using namespace std;

int main() { int numbers[5]; int *p; p = numbers ; //or p=&numbers[0] or *p=10 ; p++; *p =20; p= &numbers[2]; *p=30; p = numbers +3; *p=40; p=numbers; *(p+4) =50 ; for (int n=0; n<5; n++) cout << numbers[n] << " ";

************************************ #include <iostream> using namespace std; int main() {

return 0;

b=10; swap1(a,b); swap2(a,b); a=5; //by value b=10; swap3(&a,&b);

*********************************************** #include <iostream> using namespace std; void swap1(int x, int y) { int temp=x; x=y; y=temp; cout<<x<<","<<y<<endl; } void swap2(int&x, int&y) { int temp=x; x=y; y=temp; cout<<x<<","<<y<<endl; } void swap3(int*x,int*y) { int temp=*x; *x=*y; *y=temp; cout<<*x<<","<<*y<<endl; } int main() { int a,b; a=5; //by pointers //by reference

return 0;

*********************************************** #include <iostream> #include <cstring> using namespace std; int main(){ char str[] = "this is a test"; char *start, *end; int len; char t; len = strlen(str) ; start = str; end = &str[len-1]; while(start<end){ t= *start ; *start = *end; *end =t; start++; end -- ; } cout <<"Reversed order :"<<endl; cout << str << endl; return 0; } // here start is address

Das könnte Ihnen auch gefallen