473,406 Members | 2,549 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,406 software developers and data experts.

Constructor Statements

Hello,
I was (once again) experimenting with some CPP code using constructors. I defined a constuctors 2 different ways. I believe the end result is the same, but what causes the difference in run-time for the program. Specifically, the first code took my computer 9 seconds to run, while the second code took 15 seconds, a 66% difference. I repeated the "experiment" several times and got the same result. I'm guessing that the first code doesn't have to create objects a,b,c and then initialize them to a value, but it does both at the same time? (i.e.) they are created with the values given.
Can you guys explain the difference? Is the first more "efficient"? Are there any downsides to writing a constructor like this?

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. using namespace std;
  4. class trial{
  5.     private:
  6.     double a,b,c;
  7.     public:
  8.     trial();
  9.     void display();
  10. };
  11. trial::trial()
  12. : a(5), b(9) , c(10)
  13. { }
  14. void trial::display(){
  15.     cout<<a<<' '<<b<<' '<<c;
  16. }
  17. int main(){
  18.     for(unsigned long long int i=0; i<990000000; i++){
  19.         trial f;
  20.  
  21.     }
  22. return 0;
  23. }
The Second code is
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. using namespace std;
  4. class trial{
  5.     private:
  6.     double a,b,c;
  7.     public:
  8.     trial();
  9.     void display();
  10. };
  11. trial::trial()
  12. {a=5;
  13. b=0;
  14. c=10; }
  15. void trial::display(){
  16.     cout<<a<<' '<<b<<' '<<c;
  17. }
  18. int main(){
  19.     for(unsigned long long int i=0; i<990000000; i++){
  20.         trial f;
  21.  
  22.     }
  23. return 0;
  24. }
  25.  
Once again, Thanks for assuaging my curiosity.
Oct 26 '07 #1
1 1135
weaknessforcats
9,208 Expert Mod 8TB
Can you guys explain the difference? Is the first more "efficient"? Are there any downsides to writing a constructor like this?
You just did. And Yes. And no.

As to the third question, that could involve constructor calls on ther members. That means there coulkd be exceptions. You might want to use a function-try block in that case. But as long as you use built-in types what you have will work fine.
Nov 1 '07 #2

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

Similar topics

1
by: Chris K | last post by:
I am relatively new to C++ and hope that this question is relevant. I have spent some time at the local library and some time on dejanews, but have no decided to go ahead with my question, since...
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
3
by: Vamsi | last post by:
Hello, I am working in VC++ environment.while using gdi+, i want to use a public member variable of Bitmap. which needs to be initialized at the time of creation. and i m using this gdi+ in a...
10
by: linkspeed | last post by:
Following texts are from C# spec. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this...
6
by: Dhananjaya.R | last post by:
Hi, What will be the constructor flow of control for the below snippet. I had an hard time understanding how C# can allow initialization of member variables in class declaration. public class...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
4
by: George2 | last post by:
Hello everyone, Why visual studio does not optimize constructor in this case? I do not understand what the MSDN mentioned, if use different named object, compiler can not optimize. Why? ...
3
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
11
by: Peter Jansson | last post by:
Dear newsgroup, In the following code, it looks as though the exception thrown in the constructor is not caught properly. I get this output: ---- standard output ---- Base() constructor....
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
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
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
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,...
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...

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.