GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 180 of 185    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: 2 Last Update: August 20, 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
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 2Overall Rating: -2    
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 1   Answer is not useful 0Overall Rating: +1    

 Related Questions

 The idea behind inline functions is to insert the code of a called function at the point where the function is called. If done carefully, this can improve the application's performance in exchange 
Latest Answer : inline function is a function which extends the code when it is invoked. Otherwise is skip the code. ...

Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by applying the delete operator to a base-class pointer to the object, the base-class destructor function (matching 
Latest Answer : Virtual constructor is not build-in C++ feature but it doesn't mean its not used by devs in code and in conversations. There are many other things that doesn't exist in particular language yet, people find ways around to solve it (SingleTon, Virtual Constructor, ...

Latest Answer : No, we cannot generate a c++ source code from the binary file bcoz we can compile highlevel language to lowlevel language but not lowlevel language to highlevel languageMoparthi Aravind ...
Read Answers (3) | Asked by : suji

What will be output of the following code#includeusing namespace std;class abc{ public : void main() { cout
Read Answers (5) | Asked by : Avaited

Hey anybody help me to write code for this programwap which display year,month & day presents in a year for eg.1. no. is 14 print 2 weeks2. no is 90 print 3 months3. no is 365 print 1 yearuser can input any no
Read Answers (3) | Asked by : avingle

Latest Answer : include#includeusing namespace std;int main(){             fstream *obj = new fstream();             obj->open ("main.cpp"); ...
Read Answers (4) | Asked by : arup

I want C++ code forCreate method inside Principal mentor class called assignAssociates to assign associate mentors from faculty.txt to a course. Create a file named associates.txt store CourseId,FacultyId,Role. Overload assignAssociates function with arguments. Overloading Method1 with three arguments courseId,FacultyId,Role. Overloading Method2 with two arguments courseId,FacultyId. ( Section 6.17 from chapter 6. ) Overload methods compactly and conveniently by using function templates. Create

Latest Answer : Saurabh, I bet you are wrong; definition of ctr is "member function which doesn't return any values and called on object creation". nobody can stop you of declaring ctr private, protected or public --it might be bad design but it's working model. At the ...

Design a class for car inventory . For every new car from the manufacturer there should be minimal changes to the code.
Read Answers (1) | Asked by : yathrika

Class X{X(){}X(X &obj){} // supose '=' operator is also overloaded for this class. I am not writing code for that }main(){X a;X b = a;X c(&a);X d;d = a;}What is the difference between a, b, c, d objects ?


 Sponsored Links

 
Related Articles

jQuery Table Row Finished Code

jQuery Table Row Finished Code The Finished Code Our second example page has demonstrated table row striping highlighting tooltips collapsing expanding and filtering Taken together the JavaScript code for this page is mosgoogle geshibot lang php" document ready function var highlighted
 

jQuery Interacting with Other Code

jQuery Interacting with Other Code We learned with our sorting and paging code that we can t treat the various features we write as islands The behaviors we build can interact in sometimes surprising ways; for this reason it is worth revisiting our earlier efforts to examine how they coexist with t
 

jQuery Completed sorting and paging code

Learning jQuery The Finished Code The completed sorting and paging code in its entirety follows mosgoogle geshibot lang php" fn alternateRowColors function tbody tr odd this removeClass even addClass odd ; tbody tr even this removeClass odd addClass even ; return this; ; document
 

code

geshibot lang javascript" head This is a JS snippet from Geshi Bot" Replaces the geshi default classes with mambo joomla classes function replaceGeshiWithMamboClasses if document document getElementsByTagName Category
 

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school’s al
 

The Interview Snafu

How to turn someone else’s mistake to your advantage Your dream job is about to become reality. A recruiter gave you the heads up about the perfect position at Humungous Conglomerate, Inc. You went through five interviews as well as a battery of psychological tests mandated by their HR de
 

Winning a Job Interview with a Winning Resume

Does your resume unlock your potential, take your skills to the highest level and win you the interview and the job you want now? The job market today is highly competitive and even if you think you have what it takes to get an interview you won’t get over the line without a polished, prof
 

WinRunner Programming Concepts

If you want to create WinRunner scripts that are highly efficient, there are important programming concepts that you will want to become familiar with. Understanding these concepts will provide you with a large number of key benefits. In addition to understanding these concepts, you must also learn
 

Programming Languages Certification

IT Certification programs have several options that will offer you the best knowledge.  By learning everything that you need to know about information technology you will be able to open new doors to your career and personal business desires.  IT Certification offers several vari
 





About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape