Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// Code-01
- /// Default constructor example:
- #include<iostream>
- using namespace std;
- class Name{
- public:
- Name() // default constructor
- {
- cout<< "This is my name"<<endl;
- }
- };
- int main()
- {
- Name obj, abc, xyz;
- }
- /// Code - 02
- /// Constructor with parameter
- #include <iostream>
- using namespace std;
- class Car {
- public:
- string brand;
- string model;
- int year;
- Car(string x, string y, int z) { // Constructor with parameters
- brand = x;
- model = y;
- year = z;
- }
- };
- int main() {
- // Create Car objects and call the constructor with different values
- Car carObj1("BMW", "X5", 1999);
- Car carObj2("Ford", "Mustang", 1969);
- // Print values
- cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
- cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement