C++ Interview Questions & Answers part 3
Released in 1985, C++ is an object-oriented programming language created by Bjarne Stroustrup. C++ maintains almost all aspects of the C language, while simplifying memory management and adding several features – including a new datatype known as a class (you will learn more about these later) – to allow object-oriented programming. C++ maintains the features of C which allowed for
low-level memory access but also gives the programmer new tools to simplify memory management.
If more than just one or two else clauses are used, and all are testing the same value, consider using a switch statement.
If the body of the loop should always execute at least once, consider a do…while loop; otherwise, try to use the while loop.
Q: – How big is a class object?
A class object’s size in memory is determined by the sum of the sizes of its member variables. Class methods don’t take up room as part of the memory set aside for the object. Some compilers align variables in memory in such a way that 2-byte variables actually consume somewhat more than 2 bytes. Check your compiler manual to be sure, but at this point you do not need to be concerned with these details.
Q: – If I declare a class Cat with a private member itsAge and then define two Cat objects, Frisky and Boots, can Boots access Frisky’S itsAge member variable?
An accessor is a class operation that does not modify the state of an object in C++. The accessor functions need to be declared as const operations.
Q: – Why shouldn’t I make all the member data public?
Making member data private enables the client of the class to use the data without worrying about how it is stored or computed. For example, if the Cat class has a method GetAge(), clients of the Cat class can ask for the cat’s age without knowing or caring whether the cat stores its age in a member variable or computes its age on-the-fly.
If you are initializing a counting variable, testing that variable, and incrementing it each time through the loop, consider the for loop. If your variable is already initialized and is not incremented on each loop, a while loop may be the better choice.
Template class: A generic definition or a parametrized class not instantiated until the client provides the needed information. Class template: A class template specifies how individual classes can be constructed much like the way a class specifies how individual objects can be constructed.
Submitted By:-Ankur Goyal Email-ID: – firstname.lastname@example.org