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

Difference between this TWO

what is difference between these two ?

1.

class A
{
private:
int a;
int b;
public:
A(int i, int j)
{
a = i;
b = j;
}
};

2.

class A
{
private:
int a;
int b;
public:
A(int i, int j) : a(i), b(j)
{
}
};


can, any one explain it more....
Jun 13 '07 #1
3 1280
gpraghuram
1,275 Expert 1GB
HI,
The second example u have given is an example of constructor Argument list.
It is one of the best practices for coding in c++.
For info on it make a search and u will get it

Raghuram
Jun 13 '07 #2
With an int in a normal C++ program, you can assign a value to the int by doing:
Expand|Select|Wrap|Line Numbers
  1. int a(5);
  2.  
The second one is just another way to assign values to private variables in a class. Same way as mentioned above
They are the same, just different syntax
Jun 13 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
This is ot correct:
With an int in a normal C++ program, you can assign a value to the int by doing:

Code: ( text )
int a(5);

The second one is just another way to assign values to private variables in a class. Same way as mentioned above
They are the same, just different syntax
When you use a constructor initializer list, the values are placed in the data members as they are created. When you assign values to members inside the constructor, you are changing the value of a data member that already exists.

If your class has a const member:
Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.     private:
  4.         const int MAX;
  5.     public:
  6.         MyClass();
  7. };
  8.  
the value of the MAX member must be placed in the member when it is created. Becuse of this, the code below will produce an error:

Expand|Select|Wrap|Line Numbers
  1. MyClass::MyClass()
  2. {
  3.     MAX = 10;   //ERROR. Cannot change a constant.
  4. }
  5.  
There will also be another error about creating a const without a value.

The way around this is the initializer list:

Expand|Select|Wrap|Line Numbers
  1. MyClass::MyClass() : MAX(10)
  2. {
  3. }
  4.  
Now the value 10 is used to initialize the MAX member when the MAX member is created.

You will also need the initializer list to call constructors for any base classes, or construtors for any contained class members.

Lastly, this is bad form:

Expand|Select|Wrap|Line Numbers
  1. int a(5);
  2.  
This code is equivalent to:
Expand|Select|Wrap|Line Numbers
  1. int a = static_cast<int> (5);
  2.  
This form of initialization is in C++ for template writers.
Jun 13 '07 #4

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

Similar topics

3
by: John Harman | last post by:
Hi, I'm trying to do a MySQL Query using Mysql 3.23.58 something like that below SELECT name FROM customers WHERE name LIKE "Fred" ORDER BY difference(name,"Fred"); The difference piece...
21
by: b83503104 | last post by:
Hi, Can someone tell me the difference between single quote and double quote? Thanks
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
3
by: Materialised | last post by:
Hi, I often see 2 different declairations of the main() function, and I am unsure of the difference. They are int main(int argc, char *argv) int main(int argc, char **argv) What is the...
5
by: Agnes | last post by:
For my own practices. I like to put "Me". e.g IF Me.txtInvoice.textlength = 0 ....... etc Me.txt.....etc However, Is there any difference (without Me) ?? Thanks
7
by: raghunandan_1081 | last post by:
Hi guys, can you please tell me what is the Difference between c structure and c++ structure
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
12
by: Petronius | last post by:
Hallo, does anyone have an idea how to implement difference lists in Javascript? Thanks allot in advance
5
by: Julius | last post by:
Hej dudes, I need to calc the difference between two timestamps / dates ... For example what i need to calculate: Date 1: 2007.11.06 - 20:13:04 Date 2: 2007.11.07 - 21:13:04 Difference:...
11
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.