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 if($key==0): ?> active <?php en

JS: Cheatsheet

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

C++: 9. File Handling

Files are used to store data in a storage device permanently. File handling provides a mechanism to store the output of a program in a file and perform various operations on it. Example of opening/creating a file    #include<iostream> #include<fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file.close(); // Step 4: Closing file } return 0; } Writing to a File #include <iostream> #include <fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file_write.txt",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file<<"Learning File handling"; //Writing to file new_file.close();