Constructors in c++ - learnit

Home Top Ad

Post Top Ad

Monday, May 18, 2020

Constructors in c++

C Programming How to Use switch Statement

What is C ++ Programming

C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.


Why Learn C++ Programming

-C++ is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance.

-After learning C++, it will be much easier to learn other programming languages like Java, Python, etc.

-C++ helps you to understand the internal architecture of a computer, how computer stores and retrieves information.


Constructors (C++)

A constructor has the same name as the class and no return value. You can define as many overloaded constructors as needed to customize initialization in various ways. Typically, constructors have public accessibility so that code outside the class definition or inheritance hierarchy can create objects of the class. But you can also declare a constructor as protected or private.


Constructors can optionally take a member init list. This is a more efficient way to initialize class members than assigning values in the constructor body. The following example shows a class Box with three overloaded constructors. The last two use member init lists:


Example

class mytest{
public:
mytest()//Constructor
{
Cout<<"Hello Knowledge Education Cambodia";
}
};

int main ()
{
mytest obj;
return 0;
}

.Constructors may be declared as inline, explicit, friend or constexpr.

.A constructor can initialize an object that has been declared as const, volatile or const volatile. The object becomes const after the constructor completes.


Member initializer lists

A constructor can optionally have a member initializer list, which initializes class members prior to execution of the constructor body. (Note that a member initializer list is not the same thing as an initializer list of type std::initializer_list <T>.)Using a member initializer list is preferred over assigning values in the body of the constructor because it directly initializes the member. In the following example shows the member initializer list consists of all the identifier(argument) expressions after the colon:


Default constructors

-Default constructors typically have no parameters, but they can have parameters with default values.


-Default constructors are one of the special member functions. If no constructors are declared in a class, the compiler provides an implicit inline default constructor.


Example

#include <iostream>
using namespace std;
class mytest{
public:
mytest()//Constructor
{
Cout<<"Hello Knowledge Education Cambodia";
}
};
int main ()
{
mytest obj;
return 0;
}

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad