[英]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.如前所述,您尚未传递姓名和年龄的值。
Person::Person(int32_t age = 0, std::string name = ""){ this->age = age; this->name = name; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.