473,811 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance syntax question

I have a question about the syntax involved in inheritance. I have a parent
class, and a child class. When I create an instance of the class, I pass to
it 3 parameters. Two of them are used by the child class, one by the parent
class. I can do this:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;
}
}

And it works. I run into problems when I want to move the implementation of
the consructor outside of the class definition. If I'm not passing a parm up
to the parent, this is no problem - I simple create a Child::Child(.. .)
method outside of the class definition, and it works fine. How do you do
this and pass a parm up to the parent? If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
}

Child::Child( ....){...}

I get compile error. If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3)
}

Child::Child( ....) : Parent( parm3){...}

I likewise get compile errors. Can you not move the implementation of the
consructor outside of the class definition in a case like this? If so, can
some kind soul give me an example of the correct syntax, or point me to an
example? I've looked at a lot of examples, but have not found one case of
the implementation of the constructor being outside of the class definition
when you pass parms to the constructor, and then pass some of the parms up
to the parent class constructor.
Mar 9 '07 #1
3 1604
Zootal wrote:
I have a question about the syntax involved in inheritance. I have a parent
class, and a child class. When I create an instance of the class, I pass to
it 3 parameters. Two of them are used by the child class, one by the parent
class. I can do this:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;
}
}

And it works.
No it doesn't, it wouldn't even compile. Please post the real code you
are having problems with.

--
Ian Collins.
Mar 9 '07 #2
>
No it doesn't, it wouldn't even compile. Please post the real code you
are having problems with.

--
Ian Collins.
My bad. This compiles:

class Parent
{
public:
Parent( int parm3 ){ _parm3 = parm3; }
private:
int _parm3;
};

class Child : public Parent
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;
}

private:
int _parm1;
int _parm2;
};

int main()
{
return 0;
}
The implementation of the constructor is in the class definition. The
following has the implementation of the constructor outside of the class
definition, and it compiles. I'm not sure why it didn't before....I must
have been doing something stupid...

class Parent
{
public:
Parent( int parm3 ){ _parm3 = parm3; }
private:
int _parm3;
};

class Child : public Parent
{
public:
Child( int parm1, int parm2, int parm3);

private:
int _parm1;
int _parm2;
};

// Constructor implementation outside of class definition
Child::Child(in t parm1, int parm2, int parm3) : Parent ( parm3 )
{
_parm1 = parm1;
_parm2 = parm2;
}

int main()
{
return 0;
}
Mar 9 '07 #3
On Mar 9, 8:22 am, "Zootal" <nousenets...@z ootal.nospam.co mwrote:
I have a question about the syntax involved in inheritance. I have a parent
class, and a child class. When I create an instance of the class, I pass to
it 3 parameters. Two of them are used by the child class, one by the parent
class. I can do this:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)
{
_parm1 = parm1;
_parm2 = parm2;

}
}

And it works. I run into problems when I want to move the implementation of
the consructor outside of the class definition. If I'm not passing a parm up
to the parent, this is no problem - I simple create a Child::Child(.. .)
method outside of the class definition, and it works fine. How do you do
this and pass a parm up to the parent? If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3) : Parent( parm3)

}

Child::Child( ....){...}

I get compile error. If I try:

class Child
{
public:
Child( int parm1, int parm2, int parm3)

}

Child::Child( ....) : Parent( parm3){...}

I likewise get compile errors. Can you not move the implementation of the
consructor outside of the class definition in a case like this? If so, can
some kind soul give me an example of the correct syntax, or point me to an
example? I've looked at a lot of examples, but have not found one case of
the implementation of the constructor being outside of the class definition
when you pass parms to the constructor, and then pass some of the parms up
to the parent class constructor.
I tried out your sample and it compiles and works fine in both cases.
I used g++ 3.3 .

Cheers
-Vallabha
S7 Software Solutions
http://www.s7solutions.com/

Mar 9 '07 #4

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

Similar topics

2
4383
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
8
7832
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
12
1729
by: Taylor | last post by:
I'm trying to understand inheritance. I'd like to make my own type of IPAddress lets call it myIp. The following gives me CS0029 error: Cannot implicitly convert type 'System.Net.IPAddress' to 'Inheritance_Testing.myIp' Could you steer me in the right direction? class myIp : System.Net.IPAddress
22
23391
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
9
1747
by: bob taylor | last post by:
Imagine I have two classes A and B. Class A has the function I, function J and Attribute K. Class B has the funcion X, function Y, Attribute Z. Is it possible to establish the following Class C through interitance: Class C which has the attribute K inherited from Class A and the attribute Z inherited from Class B ?
0
1197
by: Terry Hancock | last post by:
I've been discussing PyProtocols with a a friend collaborating with me on a SF game project, about the benefits and design concept of "component architecture", and I'm a little confused by what I'm learning. I learned the concepts of "component architecture" from the Zope project, where if I'm not mistaken, "components" are essentially python "mix-in" classes, and you make a class from components by using multiple inheritance and...
60
4952
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
25
3042
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am aware of that fact that if you have a class B, that inherits from A, then List<Bdoes NOT inherit from List<A>. So I understand why the example below does not compile, but I fail to
3
1845
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.