473,799 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialised class member objects

Hi,
Im having problems initialising class member objects in the class's
constructor

e.g. My program isnt about monkeys, but there is too much code to post here.

class monkey
{
private:
int age;
public:
monkey(int);
};
monkey::monkey( int a)
{
age = a;
}
ok class monkey is pretty irrelevant, it will be a member object for person

class person{
private:
monkey ownedMonkey;
public:
person(int)
};

person::person( int monkeyAge)
{
ownedMonkey(mon keyAge);
}

basically im not sure how to initialise the owned monkey in the person's
constructor. When I try to compile my code it says there is no constructor
for monkey(void), even though im using a value.

Anyone know how to correctly initialise member objects?

Thanks,
Vipa
Jul 19 '05 #1
2 2778
"claire.bel l1" <cl**********@n tlworld.com> wrote...
Im having problems initialising class member objects in the class's
constructor

e.g. My program isnt about monkeys, but there is too much code to post here.
class monkey
{
private:
int age;
public:
monkey(int);
};
monkey::monkey( int a)
{
age = a;
}
ok class monkey is pretty irrelevant, it will be a member object for person
class person{
private:
monkey ownedMonkey;
public:
person(int)
};

person::person( int monkeyAge)
{
ownedMonkey(mon keyAge);
}

basically im not sure how to initialise the owned monkey in the person's
constructor. When I try to compile my code it says there is no constructor
for monkey(void), even though im using a value.

Anyone know how to correctly initialise member objects?


You better believe it! And soon you will be one of those who know.

Inintialiser lists exist specifically for initialising base classes
and members. You need to read up on those, but for now I will just
give you an example using your classes:

class monkey
{
int age;
public:
monkey(int a);
};

monkey::monkey( int a) : age(a) // initialise, not assign
{
}

class person
{
monkey ownedMonkey;
public:
person(int monkey_age)
};

person::person( int monkey_age) : ownedMonkey(mon key_age)
{
}

BTW, those things must be described in FAQ section 10 (IIRC).

Victor
Jul 19 '05 #2

"claire.bel l1" <cl**********@n tlworld.com> wrote in message
news:re******** ***********@new sfep2-win.server.ntli .net...
Hi,
Im having problems initialising class member objects in the class's
constructor

e.g. My program isnt about monkeys, but there is too much code to post here.

Shame.

class monkey
{
private:
int age;
public:
monkey(int);
};
monkey::monkey( int a)
{
age = a;
}
ok class monkey is pretty irrelevant, it will be a member object for person
class person{
private:
monkey ownedMonkey;
public:
person(int)
};

person::person( int monkeyAge)
{
ownedMonkey(mon keyAge);
}

basically im not sure how to initialise the owned monkey in the person's
constructor. When I try to compile my code it says there is no constructor
for monkey(void), even though im using a value.

Anyone know how to correctly initialise member objects?

Thanks,
Vipa


Which book are you reading which doesn't mention initialiser lists?

person::person( int monkeyAge) : ownedMonkey(mon keyAge)
{
}

It surprises me how many newbies ask this question. Surely every book on C++
must talk about initialiser lists?

john
Jul 19 '05 #3

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

Similar topics

2
9604
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
4
1774
by: Oystein Haare | last post by:
Is it best to declare class member objects as pointers or not pointers? class A { public: A(int i); //.... };
5
1392
by: CFF | last post by:
I am working on a VC6++ project that involve an object to be initialised by a 'this' pointer pointing to another object. I encountered, however, a syntax error. I wonder if someone can help. See the code below. What I couldn't figure out is it is OK with CMyClassA B2(this); in fileB.cpp but a SYNTAX ERROR with CMyClassA m_B2(this);
6
2545
by: gustav04 | last post by:
hi all i have a question: what is the difference between a c-function and an c++ class method (both do exactly the same thing). lets say, i have a function called print2std() and a class called CUtils with a static method called print2std()
12
2681
by: Manolis | last post by:
Hi, I was wondering if there is any way to make two objects of the same class to be able to access each other's private data, like this: class A { public: void access( const A& a ) {cout<<"a.value="<<a.value<<endl; } private: int value;
1
3535
by: Jing You | last post by:
hi every one, I have got some confused problem when I try to write some custom object by javascript. Look at the example code here: <BODY> <script language="jscript">
14
1991
by: Lee Franke | last post by:
I can't seem to figure this one out. Here is my class structure namespace name { public class foo { } }
6
3508
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which can be used by static member functions of the base class? Something like virtual static XClass* pXClass; /* pXClass shall be pure virtual, so not static in base class, but
1
3029
davydany
by: davydany | last post by:
Hey guys...a n00b Here for this site. I'm making a sequence class for my C++ class. And The thing is in the array that I have, lets say i put in {13,17,38,18}, when i see the current values for the array data from 0 to3, I get this {13, JUNK VALUE, 17,38, 18} and JUNK VALUE is like 1.8e831 or something like that. This happens when I use the attach() function and use the current() function to display the values at data I really want to...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9546
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10247
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5593
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.