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:
rint();
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