简体   繁体   English

C++ 中的 Inheritance,如何从派生的 ZA2F2ED4F8EBC2CBB14C21A29DC40 中初始化基础 class 中的成员变量

[英]Inheritance in C++, how to initialize member variables in base class from derived class

i just opened up c++ today for the first time almost, and i tried doing some inheritance.我今天几乎第一次打开了 c++,我尝试做一些 inheritance。

I have a class called Person and three classes that derive from Person called: Retiree, Adult, Child.我有一个名为 Person 的 class 和三个派生自 Person 的类:退休人员、成人、儿童。

The console asks for your age and in the case that you type 30 into the console i want to make a new adult object, and here i want to pass in the parameters: age, name and discount.控制台询问您的年龄,如果您在控制台中输入 30,我想制作一个新的成人 object,在这里我想传入参数:年龄、姓名和折扣。

In java i would just call the constructor in the child class, as it has the super(a, b, c) in it.在 java 中,我只需调用子 class 中的构造函数,因为其中包含 super(a, b, c)。 But when i try to do it here, it won't work, and i can't seem to figure out why.但是当我在这里尝试这样做时,它不起作用,我似乎无法弄清楚为什么。

Down below is two cpp files for Person and Adult showing their constructors and lastly the Main.cpp下面是 Person 和 Adult 的两个 cpp 文件,显示了它们的构造函数,最后是 Main.cpp

I get this error when i try to create the object "Unhandled exception at 0x759EA842 in LearnCPP.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00AFF514." I get this error when i try to create the object "Unhandled exception at 0x759EA842 in LearnCPP.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x00AFF514."

Person.h人.h

#pragma once
#include <String>
#include "BudgetAccount.h"
class Person
{

private:


public:
    Person(int32_t age, std::string name);

    int32_t getAge();

    void setAge(int32_t age);

    std::string getName();

    void setName(std::string name);

protected:
    int32_t age;
    std::string name;

};

Person.cpp个人.cpp

#include "Person.h"
#include <String>

Person::Person(int32_t age, std::string name)
{
    this->age = age;
    this->name = name;
}



int32_t Person::getAge() 
{

    return age;
}

void Person::setAge(int32_t age)
{
    this->age = age;
}

std::string Person::getName()
{
    return name;
}

void Person::setName(std::string name)
{
    this->name = name;
}

Adult.h成人.h

#pragma once
#include "Person.h"
class Adult : public Person
{
private:
    double discount;

public:
    Adult(double discount);
};


Adult.cpp成人.cpp

#include "Adult.h"

Adult::Adult(double discount) : Person(age, name)
{
    this->discount = discount;
}

Main.cpp主文件

#include <iostream>
#include "Person.h"
#include "Adult.h"

int main()
{
    std::cout << "Hello Customer" << std::endl;
    std::cout << "Down below you see a list of cities" << std::endl;
    std::cout << "Please enter your name" << std::endl;
    //Cin 
    std::string name;
    std::cin >> name;

    std::cout << "Please enter your age" << std::endl;
    
    std::int32_t age;
    std::cin >> age;

    //Check if the entered age is child, adult or retiree
    
    Adult user(50.0);
    
    std::cout << "Please select which city you want to travel to" << std::endl;

    return 0;
}

I think this is your problem:我认为这是你的问题:

Adult::Adult(double discount) : Person(age, name)
{
    this->discount = discount;
}

You haven't passed in an age or name to this constructor, so it's using them from the parent class -- whose constructor hasn't been called yet.您尚未将年龄或名称传递给此构造函数,因此它正在使用父 class 中的它们——尚未调用其构造函数。

As previously mentioned, you haven't passed the value for name and age.如前所述,您尚未传递姓名和年龄的值。

  1. One solution to this is that you can change the constructor to take in the values for age and name.对此的一种解决方案是,您可以更改构造函数以获取年龄和姓名的值。
  2. Another solution is that you can define default constructor.另一种解决方案是您可以定义默认构造函数。 You can also set default values in the parameterised constructor.您还可以在参数化构造函数中设置默认值。
 Person::Person(int32_t age = 0, std::string name = ""){ this->age = age; this->name = name; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++继承基类使用派生类的成员 - C++ inheritance Base class uses member of derived class C ++派生类是否可以从Base类继承静态数据成员和静态成员函数? - C++ Does derived class could inheritance Static data member and Static Member function from Base class? 基类和派生类之间的继承数据成员c ++ - inheritance data member between base and derived class c++ C++ 从基类指针访问派生类成员 - C++ Access derived class member from base class pointer 从派生类到基类的C ++成员初始化 - C++ member initialization from derived class to base class 为什么C ++继承不允许基类的公共成员继承到派生类的私有成员? - Why C++ inheritance not allow base class's public member inherit to derived class's private member? 从C++中具有多重继承的类派生的类:派生类试图调用根基类构造函数 - Class derived from a class with multiple inheritance in C++: the derived class is trying to call the root base class constructor C ++继承,从基类调用派生函数 - C++ Inheritance, calling a derived function from the base class 使用抽象基类C ++中的变量派生的类 - Derived class using variables from abstract base class C++ 如何从派生类访问派生基成员?(在C ++中) - How can I access derived base member from derived class?(in C++)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM