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

  • SEARCH
  • Home
  • 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 7907725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:36:04+00:00 2026-06-03T11:36:04+00:00

I am trying to get through the concept of Inheritance (testing in C++). Here

  • 0

I am trying to get through the concept of Inheritance (testing in C++). Here a quote from: http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

“In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, or to establish a subtype from an existing object, or both, depending upon programming language support”

Then I tested this code:

class Person
{
public:
    int ii;
    Person():ii(0){}
};
class Student : public Person
{
};
class Student1 : public Person
{
};

Then,

Person p;
Student s;
Student1 s1;
s.ii = 222;
p.ii = 333;
cout << s.ii << endl; // prints 222
cout << s1.ii << endl; // prints 0
cout << p.ii << endl; // prints 333

As shown in the results, each of the sub class has its own version of ii variable and each one the get the copy value from the base class. Therefore, when we changed one, it doesn’t affect the others.

That’s not what I got in mind at first place. I thought that when inheriting from a base class, all sub classes will inherit the same instance of attributes. Each sub class does not need to keep its own versions. And that can take the advantages of re-use and space saving.

Am I misunderstanding something? And if I am correct, is it true for other OOP languages, too?
(I know I can use static variable to reach my thought, but that’s not what I am talking about)

  • 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-06-03T11:36:09+00:00Added an answer on June 3, 2026 at 11:36 am

    You have three instances of the classes so they are independent. Meaning the subclasses don’t have to declare ii. Think of the classes like templates (don’t confuse them with the language construct template though) and then you create instances of them.

    You wouldn’t want all Persons to always have the same field values would you?

    Having said that, you might be looking for (probably not/hopefully not) static variables.

    Look up instances and classes in object orientation.

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

Sidebar

Related Questions

alt text http://img42.imageshack.us/img42/4161/blinkthru.png I'm trying to get the validation to not show through my
I'm trying using NHibernate.Search to get Lucene.NET Score through projections. My domain object implements
I am trying to fill gridpanel to get data from database through LINQ in
I am trying to produce a proof of concept shipping logging from Log4J through
I'm trying to get a file uploaded through a PHP script, but my $_FILES
Just trying to get some rows out of a database and loop through but
I am trying to get time with microseconds through glib which will work on
I'm trying to get a HTML source of a website through C# code. When
I went through the dev portal provisioning process twice now trying to get it
I am trying to get the hang of C++ pointers and objects, through a

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.