Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6912591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:06:41+00:00 2026-05-27T09:06:41+00:00

I have a method in both the base class and the derived class. When

  • 0

I have a method in both the base class and the derived class. When I call this method on an object in the derived class it calls the base class method and not the derived class method. Here is my code:

Person.h

class Person
{
...
public:
...
    virtual void coutPerson();
};

Person.cpp

void Person::coutPerson() {
    cout << name << endl;
    birthday.coutDate();
    phoneNumber.coutPhoneNumber();
    cout << city << ", " << state << endl;
}

Student.h

class Student : public Person
{
...
public:
...
    virtual void coutPerson();
};

Student.cpp

void Student::coutPerson() {
    cout << "DEBUG: Student::coutPerson()" << endl;
    //Person::coutPerson();
    cout << "Dorm Room: " << this->dorm << " " << this->dormRoom << endl;
}

Object created at: addPerson<Student>(personVector);
When object is created it is created as a Student because it calls the Student construtor.
Method called at: personVector[num-1].coutPerson();
The Student object is the one being told to coutPerson because it is displaying the info that I gave it when I created the Student object.

template<typename T> void addPerson(vector<Person> &personVector) {
    T p;
    personVector.push_back(p);
}

When the coutPerson() method is called on a Student object it only couts the name, birthday, phoneNumber, and city & state. What is wrong with this code? It should be calling the Student coutMethod…

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:06:42+00:00Added an answer on May 27, 2026 at 9:06 am

    Your personVector contains Person objects. Not references to Person objects, but actual Person objects. When you “put your Student object into the vector”, what really happens is that the vector constructs a new Person object, which is copy-initialized with the Student object you give to it, that is, it creates a copy of the Person part of the Student object. This behaviour is also known as “slicing”.

    Since there is no Student object in the vector, but only a Person object, it is no surprise that calling coutPerson on it calls Person::coutPerson, not Student::coutPerson.

    If you want polymorphic behaviour, you have to store pointers (preferably smart pointers like shared_ptr or unique_ptr) in the container. Note however that the object p you constructed in your function is destroyed on return, so for the object to survive (so you can have a pointer to it) you have to allocate it with new.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some code like this: class Base { public: virtual int Foo(int)
I have this code structure: public abstract class ContentEntryBase { public string UniqueIdentifier; public
I have a base class and a derived class both in design phase. The
Suppose both base and son class have a method method_1 , and there's another
I have a question regarding inheritance in Java. If I have this base class
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I have a base class Shapes and an extended class Circle. Both have a
Say I have the following: class Base { public Base (int n) { }
I have a base class public class A { public string s1; public string
I'm trying to compile following code in VC++ 2010: class Base { public: std::wstring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.