473,434 Members | 1,854 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,434 software developers and data experts.

Object Creation Problem

Hello All,
I was going through a very simple looking program, but as I am a newbie in C++ it created problem for me. I thought it will give compilation error but it went smoothly.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class sample
  5. {
  6.     int i;
  7.     public:
  8.         sample(int ii)
  9.         {
  10.             i=ii;
  11.         }
  12. };
  13.  
  14. class comp
  15. {
  16.       sample s;
  17.     public:
  18.         comp(int ii) : s(10)
  19.         {
  20.         }
  21. };
  22.  
  23.  
  24. int main()
  25. {
  26.     comp c(10);
  27. }
I faced following problems:
(1) In class comp object of class sample has been created but no corresponding constructor is in sample class,i.e., constructor with zero arguments, so this should create an error, but it doesn't.
(2) What is the meaning of : operator in comp(int ii) : s(10).

Please help me
thanx in advance.
Jun 2 '07 #1
2 1863
weaknessforcats
9,208 Expert Mod 8TB
I moved this thread from the C/C++ Articles to this forum.

(1) In class comp object of class sample has been created but no corresponding constructor is in sample class,i.e., constructor with zero arguments, so this should create an error, but it doesn't.
A constructor is used to initialize objects when they are created. If a class has no constructor, then then class members will be initialized according to their type.

In your case, Sample does have a constructor. Because of this, all Sample objects will require a constructor:

Expand|Select|Wrap|Line Numbers
  1. Sample obj(25);   //OK calls the Sample constructor
  2.  
  3. Sample obj1;      //ERROR: no appropriate constructor
  4.  
The : operator identifies an initializer list. The initializer list is used to place values into the class members before the { of the constructor. The comp class has a Sample member. The Sample member will created and initialized before the { of the comp constructor. In your example, this member is initialized by calling the Sample constructor with a 10.

If you remove the initializer list:

Expand|Select|Wrap|Line Numbers
  1. comp(int ii) 
  2.         {
  3.         }
  4.  
then the Sample member will still need to be initialized but the Sample class does not have a default constructor. As a result, your compiler will produce an error.
Jun 2 '07 #2
(2) What is the meaning of : operator in comp(int ii) : s(10), The : operator of a constructor is a initializer of a member variable or a base class, that is member variable s is initialized (it's constructor called) with a value of 10.

For instance
Expand|Select|Wrap|Line Numbers
  1.  class MyC
  2. {
  3. public:
  4.   int i1, i2;
  5.   MyC( int x, int y) : i1(x), i2(y) {}
  6. };
  7.  
initialized member variable i1 with x and i2 with y. If base class with constructor then this will initialize base class.

Expand|Select|Wrap|Line Numbers
  1.  class MyD : public MyC
  2.   public:
  3.  MyD( int z ) : MyC( z, z+1 ) {}
  4. };
  5. MyD d(3);
  6.  
d.i1 will equal 3 and d.i2 will equal 4.



Hello All,
I was going through a very simple looking program, but as I am a newbie in C++ it created problem for me. I thought it will give compilation error but it went smoothly.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class sample
  5. {
  6.     int i;
  7.     public:
  8.         sample(int ii)
  9.         {
  10.             i=ii;
  11.         }
  12. };
  13.  
  14. class comp
  15. {
  16.       sample s;
  17.     public:
  18.         comp(int ii) : s(10)
  19.         {
  20.         }
  21. };
  22.  
  23.  
  24. int main()
  25. {
  26.     comp c(10);
  27. }
I faced following problems:
(1) In class comp object of class sample has been created but no corresponding constructor is in sample class,i.e., constructor with zero arguments, so this should create an error, but it doesn't.
(2) What is the meaning of : operator in comp(int ii) : s(10).

Please help me
thanx in advance.
Jun 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
5
by: Lambuz | last post by:
First of all, is it possible usign .NET remoting feature inside a .NET applet loaded into a tag object inside an HTML page ? <OBJECT id="myID" height="150" width="300"...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
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
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...
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...
1
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
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.