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 8564255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:09:23+00:00 2026-06-11T17:09:23+00:00

I have a class as follows: class base { protected: int x; int y;

  • 0

I have a class as follows:

class base
{
    protected:
        int x;
        int y;
        int z;
    public:
        base(int x, int y, int z)
        {
            x = x;
            y = y;
            z = z;
        }
        virtual void show();
};

I derive a class from the above as:

class derived : protected base
{
    public:
        int a;
        int b;
        int c;
        derived(int a, int b, int x, int y, int z) : base(x, y, z) //initialising the base class members as well
        {
            cout<<a<<b<<x<<y<<z; //works fine
            a = a;
            b = b;
        }
        void show()
        {
            cout<<a<<b<<x<<y<<z; //show junk values            
        }
        //some data members and member functions
};

In main(), I use:

    derived d(1, 2, 3, 4, 5);

    d.show();

The data members appear to have legal values inside the constructor. However, when I use a similar function, i.e. with the same visibility mode, junk values seem to appear.

  • 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-11T17:09:24+00:00Added an answer on June 11, 2026 at 5:09 pm
    a = a;
    b = b;
    

    should be

    this->a = a;
    this->b = b;
    

    or, even better, use an initializer list:

    derived(int a, int b, int x, int y, int z) : a(a), b(b),  base(x,y,z) 
    {
        cout<<a<<b<<x<<y<<z; //works fine
    }
    

    what you’re doing is self-assigning the parameter, so the members don’t get set.

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

Sidebar

Related Questions

Suppose I have a Base class and its derived class Derived as follows: class
I have a POCO class as follows public class Category { public int ID
I have made a base controller class that overrides OnActionExecuting as follows: public class
I have a base Controller like follow public abstract class BaseController { protected ActionResult
I have a base class that looks as follows public class base { public
I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
I have a user and nested profile class as follows: class User < ActiveRecord::Base
I have a base class named 'baseScreen' as follows: digient.casino.ui.baseScreen = function() { goog.debug.Logger.getLogger('demo').info('baseScreen');
I have an accounts model as follows (simplified): class Account < ActiveRecord::Base attr_accessible :account_number,
I have a class A as follows: class A { public: A() { printf(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.