
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















using namespace std; class samp { int a; public : void set_a (int n) { a = n; } int get_a () { return a; } }; int main () { samp ob [4]; // four element array of objects int i; for (i=0; i <4; i++) ob[i]. set_a (i); // loads each element's a with a value between for (i=0; i <4; i++) 0 and 3 cout << ob[i]. get_a (); // prints all elements cout << "\n"; return 0; }
// Initialize an array.
using namespace std; class samp { int a; public : samp (int n) { a = n; } int get_a () { return a; }
int main () { samp ob [4] = { - 1, - 2, - 3, - 4 }; //initializing array while declearing int i; for (i=0; i <4; i++) cout << ob[i]. get_a () << ' '; // printing cout << "\n"; return 0; }
samp ob [4] = { samp ( - 1) , samp ( - 2) , samp ( - 3) , samp ( - 4) };
// Create a two - dimensional array of objects.
using namespace std; class samp { int a; public : samp (int n) { a = n; } int get_a () { return a; } }; int main () { samp ob [4][2] = { 1, 2, 3, 4, 5, 6, 7, 8, }; int i; for (i=0; i <4; i++) { cout << ob[i ][0]. get_a () << ' '; cout << ob[i ][1]. get_a () << "\n"; } cout << "\n"; return 0; }
4.2 USING POINTERS TO OBJECT
// Pointers to objects.
using namespace std; class samp { int a, b; public : samp (int n, int m) { a = n; b = m; } int get_a () { return a; } int get_b () { return b; } }; int main () { samp ob [4] = { samp (1, 2) , samp (3, 4) , samp (5, 6) , samp (7, 8) }; int i; samp *p; p = ob; // get starting address of array for (i=0; i <4; i++) { cout << p-> get_a () << ' '; cout << p-> get_b () << "\n"; p++; // advance to next object } cout << "\n"; return 0; }
4.3 THE this POINTER
using namespace std; class myclass { int a, b; public : myclass (int n, int m) { a = n; b = m; } int add () { return a+b; } void show (); }; void myclass :: show () { int t; t = add (); // call member function cout << t << "\n"; } int main () { myclass ob (10 , 14) ; ob. show (); return 0; }
using namespace std; class myclass { int a, b; public : myclass (int n, int m) { this - > a = n;
// A simple example of new and delete.
using namespace std; int main () { int *p; p = new int; // allocate room for an integer if (!p) { cout << " Allocation error \n"; return 1; } *p = 100; cout << " Here is integer at p: " << *p << "\n"; delete p; // release memory return 0; }
// Allocating dynamic objects.
using namespace std; class samp { int i, j; public : void set_i (int a, int b) { i=a; j=b; } int get_product () { return i*j; } }; int main () { samp *p; p = new samp ; // allocate object if (!p) { cout << " Allocation error \n"; return 1; } p-> set_i (4, 5); cout << " Product is: " << p-> get_product () << "\n"; return 0; }
cout << " Here is integer at p: " << *p << "\n"; delete p; // release memory return 0; }
// Allocating dynamic objects
using namespace std; class samp { int i, j; public : samp (int a, int b) { i=a; j=b; } int get_product () { return i*j; } }; int main () { samp *p; p = new samp (6, 5); // allocate object with initialization if (!p) { cout << " Allocation error \n"; return 1; } cout << " Product is: " << p-> get_product () << "\n"; delete p; return 0; }
// A simple example of new and delete
using namespace std; int main () { int *p; p = new int [5]; // allocate room for 5 integers // always make sure that allocation succeeded
if (!p) { cout << " Allocation error \n"; return 1; } int i; for (i=0; i <5; i++) p[i] = i; for (i=0; i <5; i++) { cout << " Here is integer at p[" << i << "]: "; cout << p[i] << "\n"; } delete [] p; return 0; }
// Allocating dynamic objects.
using namespace std; class samp { int i, j; public : void set_ij (int a, int b) { i=a; j=b; } int get_product () { return i*j; } }; int main () { samp *p; int i; p = new samp [10]; // allocate object array if (!p) { cout << " Allocation error \n"; return 1; } for (i=0; i <10; i++) p[i]. set_ij (i, i);
for (i=0; i <10; i++) { cout << " Product [" << i << "] is: "; cout << p[i]. get_product () << "\n"; } delete [] p; return 0; }
char *p; p = ( char *) malloc (100) ; // ... strcpy (p, " This is a test ");
using namespace std ; int main() {
char *p; p = new char[100]; // allocate memory using new // ... strcpy(p, "This is a test"); cout << p << endl; // Optional: print to verify delete[] p; // don't forget to free memory return 0; } 4.6 REFERENCES
using namespace std; void f(int *n); // use a pointer parameter int main () { int i = 0; f(&i); cout << " Here is i's new value : " << i << "\n"; return 0; } void f(int *n) { *n = 100; // put 100 into the argument pointed to by n }
using namespace std; void f(int &n); // declare a reference parameter int main () {
using namespace std; void swapargs (int &x, int &y) { int t; t = x; x = y; y = t; } int main () { int i, j; i = 10; j = 19; cout << "i: " << i << ", "; cout << "j: " << j << "\n"; swapargs (i, j); cout << " After swapping : "; cout << "i: " << i << ", "; cout << "j: " << j << "\n"; return 0; }
void swapargs (int *x, int *y) { int t; t = *x; *x = *y; *y = t; }
using namespace std; void round ( double &num );
int main () { double i = 100.4; cout << i << " rounded is "; round (i); cout << i << "\n"; i = 10.9; cout << i << " rounded is "; round (i); cout << i << "\n"; return 0; } void round ( double &num ) { double frac ; double val; frac = modf (num , &val ); /* modf() function is a pre-built function of header file if( frac < 0.5) it separates fraction and integer part from any number num =val ; modf() returns a fraction value, so we keep it in frac else the integer part is kept in val. */ num = val +1.0; // syntex: modf (number, &integer_part) }
int main () { myclass x(1) ; f(x); return 0; }
using namespace std; class myclass { int who ; public : myclass (int n) { who = n; cout << " Constructing " << who << "\n"; } ~ myclass () { cout << " Destructing " << who << "\n"; } int id () { return who; } }; // Now o is passed by reference. void f( myclass &o) { // note that. operator is still used !! cout << " Received " << o.id () << "\n"; }
int main () { myclass x(1) ; f(x); return 0; }
// This program has an error.
using namespace std; class strtype { char *p; public : strtype ( char *s); ~ strtype () { delete [] p; } char *get () { return p; } }; strtype :: strtype ( char *s) { int l; l = strlen (s)+1; p = new char [l]; if (!p) { cout << " Allocation error \n"; exit (1) ; } strcpy (p, s); }