problem with inheritance

Hi!

I don't know how I should go on to create two subclasses "TV" and "Fridge". Edevice is the superclass.
Each Edevice has a state "On" or "Off" and can be switched on or off.

(Here is a big problem the handling of the strings and to get it matched with the conceptual design of the remainder of the code)

An Edevice has a power input depending on specific characteristics (length, screen diagonal, cooling capacity).

Which procedures would you recommend to implement thePowerinput virtual? It yields as result the current power consumption of the Edevice but is implemented in the subclasses.

It's really hard for me to go on. My rudimentary knowledge comes to an end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef EDEVICE_H
#define	EDEVICE_H

class Edevice {
public:
    Edevice(const std::string& theName, 
            const std::string& theState = Off,  
            double thePowerinput);
    
    std::string getName() const;
    std::string getState() const;
    std::string getPowerinput() const;
    
    
    enum theState {Off, On};     
    
    
    std::string switchOn();
    std::string switchOff();
    
    
protected:
    std::string name;
    std::string state;
    std::string powerinput;  
    
};
#endif	/* EDEVICE_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <cstdlib>
#include <iostream>
#include <string.h>	
#include "Edevice.h"
using namespace std;

Edevice::Edevice(const string& theName, 
                 const string& theState, 
                 double thePowerinput) : 
                 name(theName), state(theState),   
                 powerinput(thePowerinput)         
{ }


    string Edevice::getName() const
    {
      return name;
    }

    string Edevice::getState() const
    {
      return state;
    }

    string Edevice::getPowerinput() const
    {
      return powerinput;
    }

    string Edevice::switchOn()
    {
        return state = "On";
    }
    
    string Edevice::switchOff()
    {
        return state = "Off";
    }
Inheritance in it's core is pretty simple.
The syntax for inheriting a super class is as follows

class derived_class_name: public base_class_name

Once that happens, your derived classes have access to all public and protected members/functions of the super class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Example {
private:
int hidden;     //Cannot be changed except by itself

protected:      //Hidden to all but the subclasses and itself
int protected;

private:
void setHidden(int x);    //Any object and subclass can use this
}

class Derived : public Example {

}
Topic archived. No new replies allowed.