473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inheritance and Overloading

If I have classes Parent, Child, and Seat as follows

class Parent{
public:
float getValue(){return 0;}
...
}

class Child: public Parent{
public:
float getValue(){return val;}
...
private:
float val;
}

class Seat{
public:
Parent* getPerson(){
return thePerson;
}
void setPerson(Parent *inPerson){
thePerson=inPerson;
}
...
private:
Parent* thePerson;
}

If i do this:

Child *c = new Child();
Seat *s = new Seat();
Child->setValue(12);
s->setPerson(c);

How would I make s->getPerson()->getValue() return 12, and not 0?

--Larry
Jul 19 '05 #1
4 2173

"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bk**********@news-int.gatech.edu...
If I have classes Parent, Child, and Seat as follows

class Parent{
public:
float getValue(){return 0;}
...
}

class Child: public Parent{
public:
float getValue(){return val;}
...
private:
float val;
}

class Seat{
public:
Parent* getPerson(){
return thePerson;
}
void setPerson(Parent *inPerson){
thePerson=inPerson;
}
...
private:
Parent* thePerson;
}

If i do this:

Child *c = new Child();
Seat *s = new Seat();
Child->setValue(12);
Did you forget to define this function?
s->setPerson(c);

How would I make s->getPerson()->getValue() return 12, and not 0?


Read up on virtual functions.
Jul 19 '05 #2

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bk**********@news-int.gatech.edu...
If I have classes Parent, Child, and Seat as follows

class Parent{
public:
float getValue(){return 0;}
...
}

class Child: public Parent{
public:
float getValue(){return val;}
...
private:
float val;
}

class Seat{
public:
Parent* getPerson(){
return thePerson;
}
void setPerson(Parent *inPerson){
thePerson=inPerson;
}
...
private:
Parent* thePerson;
}

If i do this:

Child *c = new Child();
Seat *s = new Seat();
Child->setValue(12);


Did you forget to define this function?
s->setPerson(c);

How would I make s->getPerson()->getValue() return 12, and not 0?


Read up on virtual functions.


Thanks
Jul 19 '05 #3
jeffc wrote:
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bk**********@news-int.gatech.edu...
If I have classes Parent, Child, and Seat as follows

class Parent{
public:
float getValue(){return 0;}
...
}

class Child: public Parent{
public:
float getValue(){return val;}
...
private:
float val;
}

class Seat{
public:
Parent* getPerson(){
return thePerson;
}
void setPerson(Parent *inPerson){
thePerson=inPerson;
}
...
private:
Parent* thePerson;
}

If i do this:

Child *c = new Child();
Seat *s = new Seat();
Child->setValue(12);

Did you forget to define this function?

s->setPerson(c);

How would I make s->getPerson()->getValue() return 12, and not 0?

Read up on virtual functions.

In other words, because Parent::getValue() is non-virtual, and getPerson
returns a Parent*, any reference to getValue() through getPerson() will access
Parent::getValue().

If you declare getValue() as virtual in Parent(), then it will behave in the
polymorphic manner that you desire.

Jul 19 '05 #4

"red floyd" <no*****@here.dude> wrote in message
news:SE*********************@newssvr13.news.prodig y.com...
jeffc wrote:
"Larry Lindsey" <gt*****@prism.gatech.edu> wrote in message
news:bk**********@news-int.gatech.edu...
If I have classes Parent, Child, and Seat as follows

class Parent{
public:
float getValue(){return 0;}
...
}

class Child: public Parent{
public:
float getValue(){return val;}
...
private:
float val;
}

class Seat{
public:
Parent* getPerson(){
return thePerson;
}
void setPerson(Parent *inPerson){
thePerson=inPerson;
}
...
private:
Parent* thePerson;
}

If i do this:

Child *c = new Child();
Seat *s = new Seat();
Child->setValue(12);

Did you forget to define this function?

s->setPerson(c);

How would I make s->getPerson()->getValue() return 12, and not 0?

Read up on virtual functions.

In other words, because Parent::getValue() is non-virtual, and getPerson
returns a Parent*, any reference to getValue() through getPerson() will

access Parent::getValue().

If you declare getValue() as virtual in Parent(), then it will behave in the polymorphic manner that you desire.


I've made everything virtual that needs to be virtual, but now I'm having a
problem where private variables aren't keeping their values. Does anyone
know whats going on?

for instance, in the above example, after I call c->setValue(12), were I to
call c->getValue(), it would return -8.4873e+008, which I'm pretty sure is
2's complement 0x000000 for some number of 0's.

Thanks for all of you help thus far, btw.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into...
1
by: Katie | last post by:
my one base class is a linklist class.now i'd like to inherit it by another class.The problem is that when i use the derived class i can't access the base class's pointers.classic type mismatch.how...
12
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns...
6
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
10
by: Lino Barreca | last post by:
Take a look at this code: Class clsAnagrafica Public Overridable ReadOnly Property Codice() As Integer Get Return 1 End Get End Property End Class
4
by: Shayne H | last post by:
I have a query about an intricacy of inheritance and overloading. I am coding a class inherited from System.Uri. If I look in the object browser (with hidden members shown) there are 4 overloaded...
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
7
by: =?Utf-8?B?QWxleCBDb2hu?= | last post by:
In C++, there is an easy technique to provide an overloaded Equals() method. A straightforward translation to C# causes a stack overflow. Why does b.Equals(ba) in the snippet below not understand...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
3
by: puzzlecracker | last post by:
Are static method inheritable under csharp rules, if so how can they be used? Can they be virtual? They reason I am asking is because operators are static, and I don't understand how can we...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.