What is a constructor in c++ with example?



What is a constructor in c++ with example?..

Answer / Pranaw Kumara

In C++, a constructor is a special function that's used to initialize objects. The constructor has the same name as its class and has no return type (except for the bool constructor). Here's an example:n```cppnclass Rectangle {n private:n int width;n int height;nn public:n Rectangle(int width, int height) : width(width), height(height) {}n // ... other methods and members heren};nnint main() {n Rectangle rect(5, 10); // Calling constructor to create a new objectn return 0;n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

Which command properly allocates memory a) char *a=new char[20]; b) char a=new char[20]; c) char a=new char(20.0);

1 Answers  


Is it possible for a member function to use delete this?

1 Answers  


What are all predefined data types in c++?

1 Answers  


What is c++ course?

1 Answers  


Why namespace is used in c++?

1 Answers  


What is microsoft c++ redistributable 2013?

1 Answers  


C++ program output? Explain output of this program. #include <iostream> using std::cout; using std::cin; int main() {   cout<<cout<<' ';   cout<<cin;   return 0; } It prints some address in hexadecimal. what is it?

1 Answers  


What is the use of function pointer?

1 Answers  


Is atoi safe?

1 Answers  


Can notepad ++ run c++?

1 Answers  


What is the difference between std::vector and std::list

1 Answers  


What is array in c++ pdf?

1 Answers  


Categories