Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-31-2005, 01:41 PM   #1 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Inheritance and Constructors

Hey all,

I'm having some problems with my constructors. I have a base class and an inherited class. How do constructors work in this situation?

I've tried writing a seperate constructor for my inherited class, but it wouldn't recognize any of the variables from the base class. (But functions in the inherited class would.) So then I created the same variables in the inherited class as I had in the base class. (Yeah, yeah... ) But, unsurprisingly, the constructor still doesn't work. Everything compiles fine, but if I try and create a new object of type MyInheritedClass, The object is created with the default constructor from the base class.
</stupidity>

So, if someone could point me towards a tutorial addressing this, or explain it, I'd appreciate it.

(Or shall I post the code? Is this a situation where it should work but I've written some really stupid code?)

Also, would someone recommend a new book to me? My 'Beginner' books don't address lots of things like this, (they minimally address classes, inheritance, and constructors, but don't really show how they work in many situations like this...) yet I'm still very much a noob.

Thanks!
-Stacy
Aniviel833 is offline   Reply With Quote
Old 08-03-2005, 02:17 PM   #2 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
A class variable can be in the public:, protected:, or private: section.
Private data is not inherited. An inherited class can only see it's base class's public or protected members.

When an inherited class is created, it first calls the base class's constructor.
QUantumAnenome is offline   Reply With Quote
Old 08-04-2005, 01:34 PM   #3 (permalink)
fp_unit
mike
 
Join Date: Jan 2005
Location: Ottawa, ON
Posts: 79
fp_unit is on a distinguished road
Code:
class 2dCoord
{
public:
    2dCoord(double x, double y) { x_ = x; y_ = y; }

protected:
    double x_;
    double y_;
};



class 3dCoord : public 2dCoord
{
public:
    3dCoord(double x, double y, double z) {
        // Call base-class constructor with x and y values
        2dCoord(x, y);
        z_ = z;
    }
    void print() const {
        cout << "x = " << x_ << "\ny = " << y_
             << "\nz = " << z_ << endl;
    }

protected:
    double z_;
};



int main()
{
    3dCoord t(489.2210, 5939.6667, 115.293);
    t.print();
}
That should output:

x = 489....
y = 5939.....
z = 115....

Notice the call to the base class constructor. Theres more efficient ways to do it but you should practice with simple inheritance first (like this) before getting into more advanced inheritance.

As for books, I have too many, but I like "C++ How to Program" by Deitel & Deitel, and "C++ Primer Plus" by Stephen Prata.
fp_unit is offline   Reply With Quote
Old 08-04-2005, 04:24 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
Quote:
As for books, I have too many...
my thought exactly, altho I would say you can never have too many.. But if you look at my bookcase you'd might think theres way too many programming related ones.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-10-2005, 01:50 PM   #5 (permalink)
Aniviel833
Registered User
 
Join Date: Jun 2005
Posts: 26
Aniviel833 is on a distinguished road
Thankies

Sorry it's taken me so long to reply-- I've been busy these past few weeks getting ready for school.

Anyway, thank you for the explanation. I've got everything working in a simple example, though I may be back asking questions about my actual program. (There's no way it could be this easy, right? )

Thanks especially to fp_unit for the code, and book suggestions; both are most appreciated.

-Stacy
Aniviel833 is offline   Reply With Quote
Old 08-10-2005, 04:25 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Also keep in mind:
When you create an instance of a class, mind that this order is executed:

- base class constructors
- child class constructors

on destruction:

- child class destructors
- base class destructors

Anyway, just ask if you don't get it. My coding system should be up and running soon.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 11:28 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting