4 Replies - 918 Views - Last Post: 22 June 2012 - 11:57 PM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

C++ public inheritance problem - derived class won't inherit field

Posted 22 June 2012 - 10:04 AM

Hey guys. :) I have a problem with this header file:
#include <iostream>
#ifndef COMPUTER_H_INCLUDED
#define COMPUTER_H_INCLUDED

class Computer
{
protected:
    float processer;
    int hardDisk;
};

class MacComputer : public Computer
{
public:
    MacComputer(float proc, int hdisk); whikeb
};

MacComputer::MacComputer(float proc, int hdisk) : processer(proc), hardDisk(hdisk)
{
    std::cout << "Thank you for buying a Macintosh computer." << std::endl;
}

#endif // COMPUTER_H_INCLUDED



What this is supposed to do is create a class called Computer, and then derive another class called MacComputer from Computer using public inheritance.

The problem I'm facing is that when I compile, the compiler doesn't accept line 18, saying "|18|error: class 'MacComputer' does not have any field named 'processer'|
18|error: class 'MacComputer' does not have any field named 'hardDisk'|"
, even though I publicly inherited MacComputer from Computer and Computer's fields are protected. Shouldn't this allow MacComputer to inherit those fields?
Thanks for reading this topic.

This post has been edited by hulla: 22 June 2012 - 10:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ public inheritance problem - derived class won't inherit field

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4138
  • View blog
  • Posts: 12,875
  • Joined: 25-December 09

Re: C++ public inheritance problem - derived class won't inherit field

Posted 22 June 2012 - 10:13 AM

You will either need a constructor for your Computer class that takes two parameters and then call that constructor from your derived class or put the assignments inside your derived class constructor.
MacComputer::MacComputer(float proc, int hdisk) : Computer(proc, hdisk)
{


or:
MacComputer::MacComputer(float proc, int hdisk) 
{
   processer = proc;
   hardDisk = hdisk;


Jim
Was This Post Helpful? 2
  • +
  • -

#3 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: C++ public inheritance problem - derived class won't inherit field

Posted 22 June 2012 - 11:01 AM

Oh I see, thanks Jim =D
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4138
  • View blog
  • Posts: 12,875
  • Joined: 25-December 09

Re: C++ public inheritance problem - derived class won't inherit field

Posted 22 June 2012 - 11:15 AM

If you add a Computer constructor with two arguments you may want to also add a constructor with no arguments.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: C++ public inheritance problem - derived class won't inherit field

Posted 22 June 2012 - 11:57 PM

Why would I need a Computer constructor without arguments? I want there to be an error if someone attempts to create a Computer without immediately specifying what the fields hardDisk and processer are.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1