GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 180 of 203    Print  
Fixing Code Errors
What is the problem or error with the following code snippet and how would it be fixed?

class Car {
public:
Car() { num = 1; }
private:
const int num;
};

A. You cannot change a const value. You should replace the constructor with the following code: Car(): num(1){}.
B. The problem is that the keyword "private" needs to be replaced with "protected”.
C. You cannot assign 1 to num. Simply removing the line "num = 1" will correct the problem.
D. The problem is that there is no destructor with the class. Once you add a destructor for the class, the code will be fine.



  
Total Answers and Comments: 3 Last Update: December 07, 2008     Asked by: prettyfox 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: wchang
 
Use initialization list to initialize a const  data member.

Car() : num(1) { } should fix the problem.

Above answer was rated as good by the following members:
petrofahed, santhosh.kanchanapally, yzesong, j_l_larson
August 05, 2008 07:37:21   #1  
pradnyagandhe Member Since: August 2008   Contribution: 1    

RE: Fixing Code Errors
You cannot assign 1 (anything) to num. Simply removing the line num 1 will correct the problem.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 4Overall Rating: -4    
August 20, 2008 17:14:42   #2  
wchang Member Since: August 2008   Contribution: 1    

RE: Fixing Code Errors
Use initialization list to initialize a const data member.

Car() : num(1) { } should fix the problem.

 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
December 07, 2008 09:05:44   #3  
burraganesh Member Since: December 2008   Contribution: 20    

RE: Fixing Code Errors
Using initialiser list is one way and making the data num mutable is another
 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape