c++ execution result question

Asked 2 years ago, Updated 2 years ago, 30 views

#include<iostream>
#include<string>
using namespace std;
class Car {
private:
    string carNo;
    int productYear;
public:
    inline Car() {
        string carNo = "";
        int productYear = 2016;
    }
    Car(string cn, int py);
    void setCarNo(string cn);
    void setProductYear(int py);
    void dispalyCarInfo();
};
Car::Car(string cn, int py) {
    carNo = cn;
    productYear = py;

}
void Car::setCarNo(string cn){
    cn = carNo;
}
void Car::setProductYear(int py) {
    py = productYear;
}
void Car::dispalyCarInfo() {
    cout << "vehicle number:" <<< carNo<< endl;< "year of manufacture:" << product year<< endl;
}
int main(void)
{
    CarmyCar ("52Mer 9004", 2005);
    Car frCar[3];
    string carNo;
    int py;
    cout << "Enter friend car information===============================" << endl;
    for (int i = 0; i < 3; i++) {
        cout <<i+1<<<")Vehicle number>>";
        getline(cin, carNo);
        cout << i+1 <<< ") Manufacturing year>>";
        cin >> py;
        frCar[i].setCarNo(carNo);
        frCar[i].setProductYear(py);
        cin.ignore();

    }
    cout <<endl<< "My vehicle information is ===============================" <<endl;
    myCar.dispalyCarInfo();
    cout <<endl<< "Friends' vehicle information ===============================" <<endl;
    frCar[0].dispalyCarInfo();
    frCar[1].dispalyCarInfo();
    frCar[2].dispalyCarInfo();
    return 0;
}

I wonder why the price of my friends' car information doesn't come out properly.

c++ c

2022-09-22 17:54

1 Answers

It should be as below.

void Car::setCarNo(string cn){
    //cn = carNo;
    carNo = cn;
}
void Car::setProductYear(int py) {
    //py = productYear;
    productYear = py
}


2022-09-22 17:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.