Pages

Tuesday, May 6, 2025

Constructor in OOPs

 #include<iostream>

#include<string>

using namespace std;


class Car {

    string name;

    string color;

    

    public:

        Car() {

            cout << "constructor without paramas\n";

        }

    

        Car(string name, string color) {

            cout << "constructor with params\n";

            this->name = name;

            this->color = color;

            // *this.color = color; // this is a complex syntax so try to avoid it

        }

    

    

    void start() {

        cout << "car has started..\n";

    }

    

    void stop() {

        cout << "car has stopped..\n";

        }

    

    //Getter

    string getName() {

        return name;

    }

};


int main() {

    

    Car c0; // non-parameter

    Car c1("maruti 800", "white"); // parameter

    Car c2("fortuner", "white");

    

    return 0;

No comments:

Post a Comment

how to become red on codeforces by Errichto Algorithms | explained simply in a blog

 Yes. I found an available transcript of the video and summarized it simply. The video is “How To Become Red Coder? (codeforces.com)” by Err...