What is difference constructor and function in programming C plus plus?

1 answer

Answer

1001935

2026-04-13 09:20

+ Follow

A Constructor is called when you are making a new instance of a class and member functions are functions that you can call within your class or else call using the instance of that class.

for example


class Foo {


public:

int bar;

Foo(int bar) {this->bar = bar;}

inc_bar(); {this->bar++;}

};


Foo instance(10); // Constructor is called and bar is set to 10

cout << instance.bar << endl;

instance.inc_bar(); // call the member function to increment bar

cout << instance.bar << endl;

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.