472,951 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

[BEGINNER] syntax for constructing class with members and base

class A {

public:
A(int a, int b) { this.a=a; this.b=b};
int a;
int b;
}

class B : x(3), y(3), public A(1,3)
{
public:
B(int a, int b) {this.x=a;this.y=b;}
int x;
int y;
}

void main ()
{
A aclass(3,4);
B bclass(3,4);
}
Is the code correcT?
What will be the values of B.a and B.b? Are they 3, 4 or 1,3
respectively?

Jul 23 '05 #1
7 1291
Last Timer wrote:
class A {

public:
A(int a, int b) { this.a=a; this.b=b};
int a;
int b;
}

class B : x(3), y(3), public A(1,3)
{
public:
B(int a, int b) {this.x=a;this.y=b;}
int x;
int y;
}

void main ()
{
A aclass(3,4);
B bclass(3,4);
}
Is the code correcT?
No.
What will be the values of B.a and B.b? Are they 3, 4 or 1,3
respectively?


Impossible to answer. The program is ill-formed.

V
Jul 23 '05 #2
Could you please fix the code so a base class is constructed with valid
values? Thanks!

Jul 23 '05 #3
"Last Timer" <da********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
class A {

public:
A(int a, int b) { this.a=a; this.b=b};
int a;
int b;
}

class B : x(3), y(3), public A(1,3)
{
public:
B(int a, int b) {this.x=a;this.y=b;}
int x;
int y;
} Don't confuse the declaration of base classes
with the initialization of bases and members !

class B : public A // <- declares the base class
{
public:
B(int a, int b) // <- declares the constructor
: A(1,3) //<- initialize base class(es)
, x(a), y(b) //<- initialize data members
{ } //<- empty function body
};
void main ()
{
A aclass(3,4);
B bclass(3,4);
}
Is the code correcT? B was not. What will be the values of B.a and B.b? Are they 3, 4 or 1,3
respectively?

B initializes its own members with the parameters 3 and 4.
The 'A' part of instance 'bclass' only sees the 1 and 3 passed
passed-in by the constructor of B.
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 23 '05 #4
On 8 Feb 2005 12:57:41 -0800 in comp.lang.c++, "Last Timer"
<da********@yahoo.com> wrote,
class B : x(3), y(3), public A(1,3)


Sorry, that line is boggled. Please review the syntax for class
inheritance and constructor initialization lists. They both use ':'
Jul 23 '05 #5
Last Timer wrote:
Could you please fix the code so a base class is constructed with valid
values? Thanks!


Try replacing "void main" with "int main".
The main function returns an int, always.
Anything else is wrong. Return a zero if you
don't need to return a value to the operating system.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #6
"Thomas Matthews" <Th*************************@sbcglobal.net> wrote in
message news:42**************@sbcglobal.net...
Last Timer wrote:
Could you please fix the code so a base class is constructed with valid
values? Thanks!


Try replacing "void main" with "int main".
The main function returns an int, always.
Anything else is wrong. Return a zero if you
don't need to return a value to the operating system.

--
Thomas Matthews


It's a real pet peeve of mine when people have *void main()*. It just
urks...
But as far as the return value goes, I believe the standard says that the
compiler
will return 0 if no return statement is in the main function. Only IIRC>

Nathaniel L. Walker
Jul 23 '05 #7
"Last Timer" <da********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
class A {

public:
A(int a, int b) { this.a=a; this.b=b};
int a;
int b;
}

class B : x(3), y(3), public A(1,3)
{
public:
B(int a, int b) {this.x=a;this.y=b;}
int x;
int y;
}

void main ()
{
A aclass(3,4);
B bclass(3,4);
}

void main ()
{
A aclass(3,4);
B bclass(3,4);
}
Is the code correcT?


Absolutely not. It's got so much wrong with it, I won't
even try to pick it apart, rather, I'll show you an
example which does work:

#include <iostream>

class A
{
public:
A(int arg_a, int arg_b)
: a(arg_a), b(arg_b)
{
}

int a;
int b;

void print() const
{
std::cout << "\tA::a == " << a << '\n'
<< "\tA::b == " << b << '\n'
<< '\n';
}
};

class B : public A
{
int x;
int y;

public:
/* constructor (1) */
B(int arg_a, int arg_b, int arg_x, int arg_y)
: A(arg_a, arg_b), x(arg_x), y(arg_y)
{
}

/* constructor (2) [alternative to (1)] */
B(const A& arg_A, int arg_x, int arg_y)
: A(arg_A), x(arg_x), y(arg_y)
{
}

void print() const
{
A::print();
std::cout << "\tB::x == " << x << '\n'
<< "\tB::y == " << y << '\n'
<< '\n';
}
};

int main()
{
A a(1, 2);
B b1(1, 2, 3, 4); /* calls constructor (1) */
B b2(A(1, 2), 3, 4); /* calls constructor (2) */

std::cout << "Object 'a':\n";
a.print();

std::cout << "Object 'b1':\n";
b1.print();

std::cout << "Object 'b2':\n";
b2.print();

return 0;
}

It seems you're in dire need of a good C++ textbook.
See the book review section at www.accu.org

-Mike
Jul 23 '05 #8

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

Similar topics

15
by: PhilB | last post by:
Hello experts, I am a complete beginner in C++ (although I know C). I am trying to compile the code below, and I get the following error. Can anyone explain to me my mistake? Thanks! PhilB ...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
15
by: Pelle Beckman | last post by:
Hi all, I have a few newbie questions: In function declaration what does a 'const' mean inside the parameter list ? That it won't modify the value? void MemberFunction(const int x);
9
by: olanglois | last post by:
Hi, I am not sure if I have found a compiler bug (I am using VC++.NET2003) or if this is the correct behavior defined by the language but I am sure someone can clear up my confusion. Suppose the...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
20
by: W Karas | last post by:
Would the fear factor for concepts be slightly reduced if, instead of: concept C<typename T> { typename T::S; int T::mem(); int nonmem(); };
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
3
by: Edan | last post by:
I have a base class with protected members (`Base`). The function `MakeBase()` is a member function of another class, that returns a `Base` object initialized with private members of this class. Now...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.