Class Inheritance - newbie Question

A

Al

Hi Folks,

I am reading C++ programming by Larry Ullman.

I am up to the section on Class Inheritance. The code is below. My
question is with the function setname, which is called twice by
cat.setName and dog.setName - as I read it name first is equal to
"Garfield" and then the second time it is called it is overwritten by
"Odie".

Then when name is used in cat.climb and dog.bark with std::cout << name
- by my reasoning its going to be "Odie"

Obviously this is not the case but I dont understand why???

Also why the function to set name = theName. Why not just cat.name =
"Garfield"
From a confused newbie.

-Al


=================CODE==================

//pets.cpp
#include <iostream>
#include <string>

class Pet {
public:

void eat();
void sleep();
void setName(std::string theName);

std::string name;
};

class Cat : public Pet {
public:
void climb();
};

class Dog : public Pet {
public:
void bark();
};

void Pet::setName(std::string theName) {
name = theName;
}

void Pet::sleep() {
std::cout << name << "sleeps\n";
}


void Pet::eat() {
std::cout << name << "eats\n";
}

void Cat::climb() {
std::cout << name << "climbs a tree\n";
}

void Dog::bark() {
std::cout << name << "barks\n";
}

// Start Main Function

int main() {

Cat cat;
Dog dog;

cat.setName("Garfield");
dog.setName("Odie");

cat.sleep
cat.eat
cat.climb

dog.sleep
dog.eat
dog.bark

etc........
 
D

davidrubin

Al said:
Hi Folks,

I am reading C++ programming by Larry Ullman.

I am up to the section on Class Inheritance. The code is below. My
question is with the function setname, which is called twice by
cat.setName and dog.setName - as I read it name first is equal to
"Garfield" and then the second time it is called it is overwritten by
"Odie".

Then when name is used in cat.climb and dog.bark with std::cout << name
- by my reasoning its going to be "Odie"

Obviously this is not the case but I dont understand why???

Objects of type Cat and Dog each have their own 'name'. Just because
the two classes inherit from the same base class does not mean that
they share an "instance" of the base class. In fact, each *object* of
types Cat, Dog, and Pet has its own name.
Also why the function to set name = theName. Why not just cat.name =
"Garfield"

Technically speaking, you very well could assign 'cat.name =
"Garfield"'. This is because 'name' is public. Ordinarily, you declare
data members (such as 'name') to be private, and provide accessors and
manipulators to access and change the value. This is called
encapsulation. The idea is that clients depend on the (public)
interface of your class, and not the implementation details such as the
particular name of the data member variable. For example, if I wrote in
'main',

cat.setName("Garfield");

you can change the name of the name member from 'name' to 'petName',
and I would not have to change 'main' at all. OTOH, if I write

cat.name = "Garfield";

in 'main', and then you change the name of the 'name' member, I have to
change my code in 'main' or else my program will not compile. This
simple-minded example is made more meaningful if you consider writing
code which depends on libraries whose public interface (e.g., class
definitions) are defined in header files. For example, suppose I write
a program that depends on a class called math::ComplexNumber defined in
math_complex.h, and implemented in mathlib.a. A complex number has a
real part, and an imaginary part. Now, suppose I wrote

math::Complex z;
z.r = 10;
z.i = 20;

and the next version of the math package changed the names 'r' and 'i'
to 'real' and 'imaginary'. My application would fail to compile, and I
would be very upset (because I accessed 'i' and 'r' in hundreds of
different places throughout my very very large program that flys a
rocket ship). If I had used the public interface

math::Complex z;
z.setReal(10);
z.setImaginary(20);

then changes to the implementation details of class math::Complex would
be transparant to my application.
 
A

Al

Thanks David,

A lot to digest, but essentially it answers my question. I will read it
in more detail later.

Cheers

-Al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top