473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1311
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.