Technology Advances

Classes and Objects

1. A simple program demonstrating C++ class

    Code Listing: 

// Purpose: Demonstrates the use of a simple C++ class

#include <iostream>
using namespace std;

class cl {
int i; // private by default
public:
int get_i();
void put_i(int j);
};

int cl::get_i()
{
return i;
}

void cl::put_i(int j)
{
i = j;
}

int main()
{
cl s;

s.put_i(10);
cout << s.get_i() <<endl;

return 0;
}

    Running session:

10
 
 

2. Constructor

Code listing:

// Purpose: Demonstrates the use of constructor in a C++ class

#include <iostream>
using namespace std;

// Class to represent a box
class Box
{
public:
double length;
double breadth;
double height;

// Constructor
Box(double lengthValue, double breadthValue, double heightValue)
{
cout << "Box constructor called" << endl;
length = lengthValue;
breadth = breadthValue;
height = heightValue;
}

// Function to calculate the volume of a box
double volume()
{
return length * breadth * height;
}
};

Running session:

Box constructor called

Size of first Box object is 80 by 50 by 40
Volume of first Box object is 160000
 

 

3. Using inline initialization in constructor

    Code Listing: 

// Purpose: Demonstrates the use of initialization list in a constructor

#include <iostream>
using namespace std;

// Class to represent a box
class Box
{
public:
double length;
double breadth;
double height;

// Inline initialization
Box(double lv = 1.0, double bv = 1.0, double hv = 1.0):length(lv),
breadth(bv),
height(hv)
{
cout << "Box constructor called" << endl;
}

// Function to calculate the volume of a box
double volume()
{
return length * breadth * height;
}
};

int main()
{
Box firstBox(80.0, 50.0, 40.0);

// Calculate the volume of the box
double firstBoxVolume = firstBox.volume();
cout << endl;
cout << "Size of first Box object is "
<< firstBox.length << " by "
<< firstBox.breadth << " by "
<< firstBox.height
<< endl;
cout << "Volume of first Box object is " << firstBoxVolume
<< endl;

return 0;
}

    Running session:

Box constructor called

Size of first Box object is 80 by 50 by 40
Volume of first Box object is 160000

 

4. Copy constructor 

    Code Listing: 

// Purpose: Demonstrate the use of copy constructor in C++

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

class myclass {
int *p;
public:
myclass(int i);
~myclass();
int getval() { return *p; }
};

myclass::myclass(int i)
{
cout << "Allocating p\n";
p = new int;
if(!p) {
cout << "Allocation failure.\n";
exit(1); // exit program if out of memory
}

*p = i;
}

myclass::~myclass()
{
cout << "Freeing p\n";
delete p;
}

// when this function is called, the copy constructor is called
void display(myclass ob)
{
cout << ob.getval() << '\n';
}

int main()
{
myclass a(10);

display(a);

return 0;
}

    Running session:

Allocating p
10
Freeing p
Freeing p
 
¡¡ 

5. Destructor 

    Code Listing: 

// Purpose: Demonstrate the use of destructor in C++

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

class myclass {
int *p;
public:
myclass(int i);
~myclass();
int getval() { return *p; }
};

myclass::myclass(int i)
{
cout << "Allocating p\n";
p = new int;
if(!p) {
cout << "Allocation failure.\n";
exit(1); // exit program if out of memory
}

*p = i;
}

// use destructor to free memory
myclass::~myclass()
{
cout << "Freeing p\n";
delete p;
}

void display(myclass &ob)
{
cout << ob.getval() << '\n';
}
int main()
{
myclass a(10);

display(a);

return 0;
}

 

    Running session:

Allocating p
10
Freeing p