简体   繁体   中英

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.

I have a class called Person and three classes that derive from Person called: Retiree, Adult, Child.

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.

In java i would just call the constructor in the child class, as it has the super(a, b, c) in it. 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

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

#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

#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

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

public:
    Adult(double discount);
};


Adult.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.

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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM