Showing posts with label Constructor. Show all posts
Showing posts with label Constructor. Show all posts

Default Argument Constructor in C++


This program elegantly describes the Default Argument Constructor used in C++. This type of constructor can be well used to replace the usual constructors that we create. These constructors are designed to simply give the class variables some valid value even if  a value has not been assigned in the Object declaration and initialization inside main(). 

To do this, simply write the value of the variable that you want to initialize the variable with. Now if in case a new value is assigned in the Object initializer, this default defined value will be overridden with the new value. Therefore explicit as well as implicit assigning is supported. 

Constructor Overloading Code for C++

This program show how to create an Overloaded Constructor in a C++ program. The Constructor is declared in the class date and later defined outside the class. The class date consists of three Integer variables which contain the day, month and year of the date wished to be stored. There are two constructors defined with different Signatures i.e. one is using String and the other using the three Integer values, one for each of month, date and year.

Counting the Number of Objects of a Particular Class in a C++ Program

This program demonstrates how to count the number of objects that are created for a class in C++ at runtime. The idea is to have a variable that increments and counts everytime a new object has been created of a particular class. Therefore this program features what is known as the static type of variable of the class. A static variable is one that has only copy of itself no matter how many objects are created for that class. 

Constructor & Destructor Tutorial in C++


// Using a constructor and destructor.
#include <iostream.h>
#include <conio.h>
#define SIZE 100
// This creates the class stack.
class stack
{
      int stck[SIZE];
      int tos;
      public:
      stack(); // constructor
      ~stack(); // destructor
      void push(int i);
      int pop();
};

// stack's constructor function
stack::stack()
{
      tos = 0;
      cout << "Stack Initialized\n";
}
// stack's destructor function
stack::~stack()
{
      cout << "Stack Destroyed\n";
}
void stack::push(int i)
{
      if(tos==SIZE) {
      cout << "Stack is full.\n";
      return;
}

      stck[tos] = i;
      tos++;
}

int stack::pop()
{
      if(tos==0)
      {
            cout << "Stack underflow.\n";
            return 0;
      }
      tos--;
      return stck[tos];
}

int main()
{
      clrscr();
      stack a, b; // create two stack objects
      a.push(1);
      b.push(2);
      a.push(3);
      b.push(4);
      cout << a.pop() << " ";
      cout << a.pop() << " ";
      cout << b.pop() << " ";
      cout << b.pop() << "\n";
      getch();
      return 0;
}

Constructor Overloading using Rectangles in C++


#include<iostream.h>
#include<conio.h>

class CRectangle
{
    int width, height;
  public:
    void set_values (int, int);
    int area (void) {return (width * height);
      }
};

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

int main ()
{
  CRectangle a, *b, *c;
  CRectangle * d = new CRectangle[2];
  b= new CRectangle;
  c= &a;
  a.set_values (1,2);
  b->set_values (3,4);
  d->set_values (5,6);
  d[1].set_values (7,8);
  cout << "a area: " << a.area() << endl;
  cout << "*b area: " << b->area() << endl;
  cout << "*c area: " << c->area() << endl;
  cout << "d[0] area: " << d[0].area() << endl;
  cout << "d[1] area: " << d[1].area() << endl;
  return 0;
}

Constructor Overloading


//constructor overloading II
//econstr4.cpp to demo multiple constructors.

#include<iostream.h>
#include<conio.h>
class time
   { int   hrs;
     int mins;
     int secs;
     public:
          time()   {hrs=0;mins=0;secs=0;}
          time(int h,int m,int s);
          time(long h) ;
          void gettime();
          void showtime();
    };
main()
{
      clrscr();
      cout<< "econstr4.cpp start" <<endl;
      time t1;                                          //----1----
      cout<<"showing t1:";
      t1.showtime();
      time t2(9,15,0);                                  //----2----
      cout<<  "college opens        time  t2";
      t2.showtime();
      time t3;                                          //----3----
      cout<<"lanch breack starts    time  t3";
      t3.showtime();
      time t4(51300);                                   //----4----
      cout<<"lunch breack ends      time  t4";
      t4.showtime();
      time t5 = time(17,15,0);                         //----5----
      cout<<"the day ends           time  t5";
      t5.showtime();
      getch();
      return 0;
}
  time :: time(int h,int m,int s)
    {
            hrs = h; mins = m; secs = s;
    }
  time ::  time(long h)
    {
            long temp;
            secs = h % 60;
            temp =h/ 60;
            mins = temp % 60;
            hrs = temp / 60;
    }

  void time::gettime()
     {
             cout<<"enter time as hh mm ss:";

         cin>> hrs >> mins >>secs;
     }

  void time :: showtime()
     {
             cout<< hrs << ':' << mins <<':' << secs <<endl;
     }

Constructor # 1

#include <iostream.h>
#include <conio.h>
class pwr
{
double b;
int e;
double val;
public:
pwr(double base, int exp);
double get_pwr()
{
return val;
}
};
pwr::pwr(double base, int exp)
{
b = base;
e = exp;
val = 1;
if(exp==0) return;

for( ; exp>0; exp--)
val = val * b;
}

int main()
{
      clrscr();
      pwr x(4.0, 2), y(2.5, 1), z(5.7, 0);
      cout << x.get_pwr() << " ";
      cout << y.get_pwr() << " ";
      cout << z.get_pwr() << "\n";
      getch();
      return 0;
}
Custom Search