Skip to main content

C++: 6. Constructors

A constructor is a special type of member function that is called automatically when an object is created.

In C++, a constructor has the same name as that of the class and it does not have a return type.

C++ Default Constructor
A constructor which has no argument is known as default constructor. It is invoked at the time of creating object.

Example:- 
#include <iostream>  
using namespace std;  
class Employee  
 {  
   public:  
        Employee()    
        {    
            cout<<"Default Constructor Invoked"<<endl;    
        }    
};  
int main(void)   
{  
    Employee e1; //creating an object of Employee   
    Employee e2;   
    return 0;  
}  
Output:- 
Default Constructor Invoked 
Default Constructor Invoked 


C++ Parameterized Constructor
A constructor which has parameters is called the parameterized constructor. It is used to provide different values to distinct objects.

Example:- 
#include <iostream>
using namespace std;
class Employee {
   public:
       int id;//data member (also instance variable)    
       string name;//data member(also instance variable)
       float salary;
       Employee(int i, string n, float s)  
        {  
            id = i;  
            name = n;  
            salary = s;
        }  
       void display()  
        {  
            cout<<id<<"  "<<name<<"  "<<salary<<endl;  
        }  
};
int main(void) {
    Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee 
    Employee e2=Employee(102, "Nakul", 59000); 
    e1.display();  
    e2.display();  
    return 0;
}
Output:- 
101  Sonoo  890000
102  Nakul  59000

Copy Constructor in C++
The copy constructor in C++ is used to copy data of one object to another.

Example:- 
#include<iostream>
using namespace std;
class Demo {
   private:
   int num1, num2;
   public:
   Demo(int n1, int n2) {
      num1 = n1;
      num2 = n2;
   }
   Demo(const Demo &n) {
      num1 = n.num1;
      num2 = n.num2;
   }
   void display() {
      cout<<"num1 = "<< num1 <<endl;
      cout<<"num2 = "<< num2 <<endl;
   }
};
int main() {
   Demo obj1(10, 20);
   Demo obj2 = obj1;
   obj1.display();
   obj2.display();
   return 0;
}
Output:- 
num1 = 10
num2 = 20

num1 = 10
num2 = 20

Comments

Popular posts from this blog

WP: 8. Dynamically show post taxonomies and their data in tab format [Solved]

    Today I ran into a problem, I need to dynamically show post taxonomies and their data in tab format and found this solution. And nonetheless, It's better to fix the thing with some bunch of code rather than uploading one more bulky plugin on your website that will end up making your site heavy. Just follow the steps below and avoid heading up into any malicious or bulky plugin for this particular problem from now on forever. Step 1: Get the taxonomies terms first <?php     $terms = get_terms( array(       'taxonomy' => 'off_plan_categories',       'hide_empty' => true,     ) );     // echo '<pre>';     // print_r($terms);     // die; ?> Step 2:  Execute loop first for tab navigation <nav id="offPlanPropertyTabs" class="offplan-property-type-tabs nav nav-tabs"> <?php foreach($terms as $key => $term): ?>   <button class="nav-item nav-link <?php...

JS: Cheatsheet

   Datatypes Difference Between Var, Let and Const Functions and its types Asynchronous operations in JS

Py: 14. Python GUI

Python provides the standard library Tkinter for creating the graphical user interface for desktop-based applications. Developing desktop-based applications with python Tkinter is not a complex task. An empty Tkinter top-level window can be created by using the following steps. Exercises:-  Write a py program to create a window with a simple message. Write a py program to demonstrate Grid. Write a py program to generate a button. Write a py program to take user input in GUI format. Write a py program to perform arithmetic operation in GUI format.