[BEGINNER] syntax for constructing class with members and base

L

Last Timer

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?
 
V

Victor Bazarov

Last said:
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
 
L

Last Timer

Could you please fix the code so a base class is constructed with valid
values? Thanks!
 
I

Ivan Vecerina

Last Timer said:
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.
 
D

David Harmon

On 8 Feb 2005 12:57:41 -0800 in comp.lang.c++, "Last Timer"
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 ':'
 
T

Thomas Matthews

Last said:
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
 
N

Nathaniel L. Walker

Thomas Matthews said:
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.

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
 
M

Mike Wahler

Last Timer said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top