How to Make Constructor in C++ Program

Constructors are used to initialize the objects when they are created. A constructor could be overload many times as increasing its Parameters. Constructor has same name which has class name and it has no data types . If we doesn't create a constructor in any class then compiler create a default constructor.

Source Code

#include<iostream>
using namespace std;

 class LEP
 {
  public:
   LEP();
   void set_name (string n);
   string get_name();
       private:
  string name;
 };
  LEP::LEP()
 {
  cout<<" \t \t \t  Hi Visitor ! \n \n ";
 }
 void LEP:: set_name(string n)
 {
  name = n;
 }
 string LEP::get_name()
 {
  return name;
 }
int main ()
{
 LEP L;
 L.set_name(" \t We Welcome You to www.ancodingpoint.com ! ");
 cout<<L.get_name();
 cout<<endl<<endl;
 }

Output of the Program

How to make constructor in C++ PROGRAM

Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment