By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

[BEGINNER] syntax for constructing class with members and base

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
Could you please fix the code so a base class is constructed with valid
values? Thanks!

Jul 23 '05 #3

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.