Class and object 


Contents of this section:

  1. A simple program demonstrating C++ class
  2. Constructor
  3. Using inline initialization in constructor
  4. Copy constructor
  5. Destructor

1. A simple program demonstrating C++ class

    Code Listing: 

// File Name: ~ftp/pub/class/cplusplus/Class/SimpleClass.cpp
// 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:46am merc-hu[90]% CC -LANG:std SimpleClass.cpp -o SimpleClass
@10:46am merc-hu[91]% SimpleClass 
10
@10:46am merc-hu[92]% 

2. Constructor

Code listing:

// File Name: ~ftp/pub/class/cplusplus/Class/Constructor.cpp
// Purpose:   Demonstrates the use of constructor in a C++ class
// Author:    Ivor Horton, Beginning Visutal C++ 6

#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:

 
@11:12am merc-hu[115]% CC -LANG:std Constructor.cpp -o Constructor
@11:13am merc-hu[116]% Constructor
Box constructor called

Size of first Box object is 80 by 50 by 40
Volume of first Box object is 160000
@11:13am merc-hu[117]% 

3. Using inline initialization in constructor

    Code Listing: 

 
// File Name: ~ftp/pub/class/cplusplus/Class/Inline.cpp
// Purpose:   Demonstrates the use of initialization list in a constructor 
// Author:    Ivor Horton, Beginning Visutal C++ 6

#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:

@9:48am merc-hu[132]% CC -LANG:std Inline.cpp -o Inline
@9:49am merc-hu[133]% Inline
Box constructor called

Size of first Box object is 80 by 50 by 40
Volume of first Box object is 160000
@9:49am merc-hu[134]% 

กก

4. Copy constructor 

    Code Listing: 

 
// File:    ~ftp/pub/class/cplusplus/Class/CopyCons.cpp
// 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:

@10:17am merc-hu[153]% CC -LANG:std Copycons.cpp -o Copycons
@10:18am merc-hu[154]% Copycons
Allocating p
10
Freeing p
Freeing p
@10:36am merc-hu[155]% 

กก

5. Destructor 

    Code Listing: 

// File:    ~ftp/pub/class/cplusplus/Class/Destructor.cpp
// 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:

@10:43am merc-hu[157]% CC -LANG:std Destructor.cpp -o Destructor
@10:44am merc-hu[158]% Destructor
Allocating p
10
Freeing p
@10:44am merc-hu[159]% 

กก



Last modified: Friday, 21-Aug-2020 15:28:15 CST
Copyright 2000 Department of Computer Science, University of Regina.

 CS Dept Home Page

Teaching Material

C++ Index Page