class(h) won't work

hers what I did so far
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
39
40
41
42
43
44
45
46
#include <iostream>

using namespace std;

class Adress {
protected:
string name;
int age;
public:
void setAge (int a)
    { age=a;}
void setName (string a)
    { name=a;}
int getAge (){
       return age;
}
    string getName (){
       return name;
}


};
    
class Person: public Adress {
protected:
int homeNumber;
  public:
void setHomeNumber (int a){
	homeNumber=a;
};
	int getHomeNumber(){
        return homeNumber;
	}
};

int main () {
  string name;
  Person obj1, obj2;
  obg1.setHomeNumber (8);
  obj1.setAge (17);
  obj1.setName ("isaac");
  cout << obj1.getHomeNumber() << "\n";
  cout << obj1.getName() << "\n";
  cout << obj1.getAge() << "\n";
  system("pause");
}
Last edited on
line 39 is not correct.
and you are missing the rule of three.
http://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29
Hi,

There are 3 types of relationships with classes, "IS A" implies public inheritance, "HAS A" means a class has a member variable that is a different class, and "USES A" means a Class function has a parameter which is a different class.

So, in your case a Person should HAVE An address. So you don't need inheritance for this.

Btw, don't use plurals for your class names, because a class is used to instantiate ONE object. A container of objects can have a plural name though.

Don't have protected member variables, make them all private, and provide an interface for your class (but don't have trivial get / set functions for everything). Get functions by themselves are not bad, but only provide them if you need them.

Provide constructors for your classes.

Make use of initialiser lists to set values of member variables initially.

With class functions, think about what the object needs to do, using verbs - make these the function names.

If you want to print out all the details of an object, then write a function to do that, rather than calling a bunch of get functions.

Hope all is well.
the reason why I'm using inheritance in this class is because I'm mainly trying to understand how to use inheritance

are you saying that I don't protected even if I'm trying to do inheritance

and I don't understand how the rule of three is causing line 39 to be incorrect.
line 39 is incorrect because you are calling obg1, instead of obj1.
Ohh how did I not notice that
thank you

now I see they were giving me helpful suggestions
well thanks god bless
Hi,

the reason why I'm using inheritance in this class is because I'm mainly trying to understand how to use inheritance


Ok, so choose an example that does need inheritance, like geometry shapes, animals etc - I am sure you can Google up some more examples.

are you saying that I don't protected even if I'm trying to do inheritance


Yes, according to Scott Meyers, one shouldn't have public member variables, and the arguments against them are the same as for having protected member variables. Have a read of the tutorial at the top left of this page, about how to make an interface for a class, and calling base class constructors, amongst other things.

and I don't understand how the rule of three is causing line 39 to be incorrect.


Bdanielz was saying that line 39 was wrong and you also need the rule of 3.

Hope this helps :+)
Topic archived. No new replies allowed.