https://[Log in to view URL]
#include <iostream>
class MyClass {
public:
int x;
MyClass(int val) {
x = val;
}
void print() {
std::cout << "x = " << x << std::endl;
}
};
int main() {
// Create an object of MyClass and initialize its member variable
MyClass obj(10);
obj.print(); // Outputs: x = 10
// Create another object of MyClass using the default constructor
MyClass obj2;
obj2.x = 20;
obj2.print(); // Outputs: x = 20
return 0;
}
/*
In this example, the class MyClass has a constructor that takes an integer argument
val, and it initializes the member variable x with the value of val. In the main
function, the program creates an object obj of type MyClass and calls its
constructor with an argument of 10, which initializes the member variable x with
the value of 10. Then it calls the print() method on the object to print the
value of x.
Then the program creates another object obj2 of type MyClass by calling the default
constructor, which is automatically generated by the compiler because no other
constructors are defined, this object is not initialized. Then the program assigns
the value 20 to x member variable of obj2 and calls the print() method on the
object to print the value of x.
It's important to note that constructors do not return any value, and they are
automatically called when an object of the class is created. They are used to
initialize the data members of the new object and set them to a specific state
or value. They can also be used to allocate resources or perform other setup
tasks that are required for the object to function correctly.
________________________________________________________________________________
constructor in c++
In C++, a constructor is a special member function of a class that is automatically
called when an object of that class is created. The main purpose of a constructor
is to initialize the data members of the new object.
A constructor has the same name as the class, and it does not have a return type,
not even void.
Here is an example of a class with a constructor:
Copy code
class MyClass {
public:
int x;
MyClass(int val) {
x = val;
}
};
In this example, the class MyClass has a constructor that takes an integer argument
val, and it initializes the member variable x with the value of val. To create
an object of this class and initialize its member variable, you can do the
following:
Copy code
MyClass obj(10);
This creates an object obj of type MyClass and calls its constructor with an
argument of 10, which initializes the member variable x with the value of 10.
C++ also provides a default constructor if no constructor is defined. A default
constructor is a constructor that takes no arguments and has an empty body,
it's automatically generated by the compiler if no other constructors are defined.
It's worth noting that C++ also provide copy constructor, move constructor,
copy-assignment operator and move-assignment operator, which are used when the
object is copied or moved.
To embed this project on your website, copy the following code and paste it into your website's HTML: