【问题标题】:I am trying to create a vehicle registration portal. For some reason the get() is only receiving the owner name我正在尝试创建一个车辆登记门户。出于某种原因,get() 只接收所有者名称
【发布时间】:2021-07-01 20:56:55
【问题描述】:

我正在尝试创建一个车辆登记门户。出于某种原因,get() 只接收所有者的姓名。

我正在尝试实践继承并使用从父类“车辆”继承属性的概念。 这个是子类“get info”

#include<iostream>
using namespace std;

class vehicle{
    private:
        int noPlate;
        char owner;
        int registrationNo;

    public:
        int modelNo;

        void setnoPlate_setRegNo_setOwner( char number, int rNumber, char ownerName){
            noPlate = number;
            registrationNo = rNumber;
            owner = ownerName;
        }

        void displayInfo(){
            cout<<"***Vehicle Information Portal***";
            cout<<"              -----------           \n";
            cout<<"Owner Name: "<<owner<<"\n";
            cout<<"Registered Number Plate: "<<noPlate<<"\n";
            cout<<"Registration Numaber: "<<registrationNo<<"\n";
        
        }
};

继承车辆类别

class getInfo : public vehicle{
    public:
        void get(){
            char tempNoPlate;
            int tempRegNumber;
            char tempOwnerName;
            cout<<"\n***Vehicle Registration Portal*** \n";
            cout<<"              -----------           \n";

            cout<<"Enter your Registratered Owner name for your vehice:"<<"\n";
            cin>>tempRegNumber;
        
            cout<<"Enter your number plate of your vehice:"<<"\n";
            cin>>tempNoPlate;

            cout<<"Enter your Registration Number of your vehice:"<<"\n";
            cin>>tempRegNumber;

            setnoPlate_setRegNo_setOwner(tempNoPlate, tempRegNumber, tempOwnerName);

        }

        void display(){
            displayInfo();
        }

    
};

主要功能

int main(){

    getInfo vehicleInfo;
    vehicleInfo.get();
    vehicleInfo.display();
    return 0;
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。请不要同时使用cc++ 标签来标记问题,除非你真的很喜欢投票——它会惹恼人们,除非问题是关于 C 和 C++ 的交互作用,而你所质疑的似乎不是。代码显然是 C++ 代码而不是 C,所以我已经为您删除了 C 标记。

标签: c++ class object inheritance


【解决方案1】:
  • void get() 中,您试图在 int 类型的注册号中获取所有者名称,因此它会中断

  • 其次,您的 Owner Name 应该是除 Char 以外的字符串类型,因为 char 只能存储 1 个字符。

  • 在类vehicle中,noPlate是int类型,你还必须在派生类中取int类型tempNoPlateget info

这是正确的代码

#include<iostream>
using namespace std;
class vehicle{
    private:
        int noPlate;
        string owner;//Change in owner data type
        int registrationNo;

    public:
        int modelNo;

        void setnoPlate_setRegNo_setOwner( int number, int rNumber, string ownerName){ //Change in ownername data type
            noPlate = number;
            registrationNo = rNumber;
            owner = ownerName;
        }

        void displayInfo(){
            cout<<"***Vehicle Information Portal***";
            cout<<"              -----------           \n";
            cout<<"Owner Name: "<<owner<<"\n";
            cout<<"Registered Number Plate: "<<noPlate<<"\n";
            cout<<"Registration Numaber: "<<registrationNo<<"\n";

        }
};
class getInfo : public vehicle{
    public:

        void get(){
            int tempNoPlate; //this should be of int type instead of char
            int tempRegNumber;
            string tempOwnerName; // change
            cout<<"\n***Vehicle Registration Portal*** \n";
            cout<<"              -----------           \n";

            cout<<"Enter your Registratered Owner name for your vehice:"<<"\n";
            getline(cin, tempOwnerName); // change,  GETLINE here is used to read space in name, you can use cin >> tempOwnerName but
                                        // it wouldnt read space character.

            cout<<"Enter your number plate of your vehice:"<<"\n";
            cin>>tempNoPlate;

            cout<<"Enter your Registration Number of your vehice:"<<"\n";
            cin>>tempRegNumber;

            setnoPlate_setRegNo_setOwner(tempNoPlate, tempRegNumber, tempOwnerName);

        }

        void display(){
            displayInfo();
        }


};

int main(){

    getInfo vehicleInfo;
    vehicleInfo.get();
    vehicleInfo.display();
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2019-07-31
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多