How To Create Constructor In C++:

How To Create Constructor In C++:

Share It With Your Firends:

In C++ a constructor is a special method of a class or structure in object-oriented programming that initializes a newly created object of that type. Whenever an object is created, the constructor is called automatically. Constructor is used to initializing objects of a class and allocate appropriate memory to objects. That is, it is used to initialize the instance variables of a class with a different set of values but it is not necessary to initialize.

Constructor Types:

  • Default Constructor.
  • Parameterized Constructor.
  • Copy Constructor.
  • Static Constructor.
  • Private Constructor.

Example Of A Constructor:

#include <iostream>
#include <string>
using namespace std;

class Employee
    {
        public: //private: , protected:
        string name;
        int salary;

        Employee(string n, int s, int sp)
        {
            this->name = n;
            this->salary = s;
            this->secretPassword = sp;
        }

        void printDetails()
    {
        cout<<"The name of our first employee is "<< this->name << " and his salary is " << this->salary<<" dollers"<<endl;
    }
    void getsecretPassword()
    {
        cout<<"The secret password is "<<this->secretPassword<<endl;
    }
    private:
    int secretPassword;
    };
        
    class programmer : public Employee  //inheritance
    {
        public:
        int errors;
    };



int main()
{
    Employee ak("akshat constructor", 3000000, 23334);
    ak.printDetails();
    ak.getsecretPassword();
    return 0;
}

There are no โ€œreturn valueโ€ statements in the constructor, but the constructor returns the current class instance.

Share It With Your Friends

Leave a Reply

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python
DIY AI Voice Assistant In Python

September 8, 2023

Build your own AI assistant with Python! Learn to recognize voice commands, generate responses, and more. Create your virtual companion…

share it

Recent Posts

  • All Post
  • A.I.
  • AI
  • c
  • c++
  • computer
  • cryptocurrency
  • database
  • digital marketing
  • finance
  • hacking
  • HTML
  • java
  • Marketing
  • network
  • other
  • programming
  • python
  • react
  • social
  • Tools
  • Uncategorized
  • web devlopment
    •   Back
    • drawing In Python

Additional Content

CodeWithAM

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus luctus.

Products

Automated Chatbot

Data Security

Virtual Reality

Communication

Support

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

ยฉ 2023 Codewitham