210: Create a class representing a car with private data members for company, model, and year. Implement member functions outside the class to set and get these attributes. Use constructor overloading to allow the creation of car objects with default, partial, or full information

 

// Create a class representing a car with private data members for company, model,
// and  year.  Implement  member  functions  outside  the  class  to  set  and  get  these
// attributes. Use constructor overloading to allow the creation of car objects with
// default, partial, or full information

#include <iostream>
using namespace std;
class Car
{
private:
    const string company, model;
    const unsigned int year;

public:
    Car();
    Car(string, string, int);
    void getData();
};
Car ::Car() : company(""), model(""), year(0) {}
Car ::Car(string x, string y, int z) : company(x), model(y), year(z) {}
void Car ::getData()
{
    cout << "Company: " << company << endl
         << "Model: " << model << endl
         << "Year: " << year << endl;
}

int main()
{
    Car c1;
    c1.getData();
    cout << endl;
    Car c2("Toyota", "Corolla", 2015);
    c2.getData();

    return 0;
}


Comments

Popular posts from this blog

215: Create a C++ program that uses single inheritance to model a simple shape hierarchy. Implement a base class "Shape" with a virtual function "printArea." Derive classes "Rectangle" and "Triangle" from "Shape" and override the "printArea" function to calculate and display their respective areas.

42: ***(Syntax)*** Calculating number of characters and spaces in a string 1- Make a program to calculate the number of characters in a string 2- Modify this and also tell how many spaces are there in that sentence

96: ***(Syntax)*** Functions and Global Variables