Você está na página 1de 6

03.

Class and Objects


Q. Define a class? or, Illustrate briefly a class in C++?
Ans: A class is a way to bind the data describing an entity and its associated functions together. In
C++, a class makes a user-defined data type having both data and information that is used to create
objects of this type.
The internal data of a class is called data member and the functions are called member functions.
The member functions mainly manipulate the internal data members of a class.A class defination con-
sists of two parts viz. header, that specifies the class name and the body, that defines the class member.
A class members fall under one of the three different access permission category:
i. Public members, accessible by all the class users
ii. Private members, accessible only by all the class members
iii. Protected members, accessible only by the members of the same class and its derived class.
Q. What is the significance of a class? or, What is the need of a class in real world?
Ans: Real world entities not only possess characteristics but also have an associated behaviour. A
class has the ability to bind the data, describing the characteristics of the real world-entity, and its asso-
ciated functions, describing the behaviour of the real world-entity, under one roof. Thus, a class can hold
the real world-entities effectively, thereby, making the software more closer to the real-world.
Q. How does a class enforces
a) Abstraction: A class, gives the essential and necessary information to the outside world only through
its public members, rest remain hidden. Thus it enforce abstraction.
b) Data-hiding: A class group its member in three sections-private, public and protected. The private
and the protected members are hidden from outside world and thus enforce data-hiding.
c) Encapsulation: A class binds together data and its associated functions under one unit thereby en-
forcing encapsulation i.e. wraps up the data and associated functions together under one unit.
Q. What are different attributes associated in declartion of class?
Ans: The declaration of a class involes declaration of its four associated attributes:
i. Data Members, the data type properties that describe the characteristics of a class.
ii. Member Functions, the set of operations that may be applied to objects of that class.
iii. Program Access Levels, control access to members from within the program.
iv. Class Tagname, serves as a type specifier for the class using which objects of this class
type can be created.
Q. What do you mean by access specifier? What are the different access specifiers in C++?
Ans: Access specifiers are keywords in Object Oriented Languages that set the accessebility of classes,
methods and other members. It facilitate the encapsulation of components. In C++, there are only three
access specifiers private, public and protected.
Q. What is the significance of access specifiers in a class? Differentiate between private and public
access specifier?
Ans: A class provides three access specifiers namely private, public and protected.
The private and the protected members are hidden from outside world and can be accessed only
by the member functions and friend functions of the class.
A member declare as public is only available to the outside world and hence can be accessed by
any functions or expression in the program using an object of the same class.
Thus, a class, through these access specifiers enforce data-hiding and abstraction, the OOPs
concept.

Page 1
Q. What do you mean by member function? How does a member function differ from an ordinary
function?
Ans: The function defined within a class scope is termed as member function, containing a set of
operations that may be applied to objects of that class.
Difference between member function and a ordinary function
i. Member functions have full access privilege to both the public and private members of the
class while, in general, ordinary functions have access only to the public members of the class.
ii. Member functions are defined within the class scope i.e. they are not visible to outside
world while ordinary functions are visible outside the scope of the class.
iii. Member functions needs to be invoked by a class object or from inside class, where as,
ordinary functions are invoked directly by its name outside the scope of the class.
Q. What are the different categories of the member functions of a class?
Ans: The member functions of a class can be categorized as:
i. Accessor function: These are the member functions that allow to access the data member of
the object but cannot change the value. In general, accessor are used to read the private members of a
class which are directly not accessible in non member function.
ii. Mutator function: These are the member functions that allow us to change the data mem-
bers of an object.
iii. Manager/Managerial function: These are member functions with specific functions that
deal with initializing and destroying class instances e.g. constructors and destructors.
Q. Why accessors and mutators are necessary in a class definition?
Ans: By declaring all the elements of a class a public, data becomes unsafe and vulnerable, violating
the very purpose of object orientation. Thus, by providing accessors and mutators, a class ensures that
data is edited in desired manner through a mutator and values of private data members can be get
through accessor.
Q. How does a class member functions (methods) defined?
Ans: A class member function (method) can be defined inside as well as outside the class definition.
i. Inside a class, the function is defined at the place of declaration. This function is treated as
inline function. The coding of normal member function and inline function is similar except that inline
function definition starts with the keyword inline.
ii. Outside a class, a function is defined with a qualified name as
return-type class-name : : function-name (parameter list){
//function body
}
where class-name indicates that the function specified by function-name is a member of
the class specified by the class-name. The symbol ::, called scope resolution operator, specifies the
scope of the function is restricted to the class specified by the class-name.
Q. What do you mean by scope resolution operator? What is its significance?
Ans: A scope resolution operator ::, is the symbol, denoted as ::, used while defining a member func-
tion outside the class definition with a qualified name, specifies that the scope of the function defined is
restricted to the class specified. The :: operator uncovers a hidden file scope (global) item.
Q. What is an inline function?
Ans: A member function definition that starts with the keyword inline is called an inline function. It is
a C++ enhancement designed to speed up the program.

Page 2
Q. How an inline function speeds up the program? or, What is the significance of an inline func-
tion?
Ans: With inline code, the C++ compiler replaces the function call statement with the function code
itself, termed as expansion, and then compiles the entire code. As such function calling overheads are
saved. Thus, it speeds up the program execution.
Q. State the advantage and disadvantage of an inline function?
Ans: Advantage: Inline function runs faster than the normal function as the inline function call state-
ment gets replaced with the function code itself saving the function calling overheads.
Disadvantage: Inline function causes a memory penalty because every time an inline function is
called, it inserts a copy of the function in the code. Repeated occurrences of same function code waste
the memory space.
Q. State the difference between inline and ordinary function?
Ans: i. The definition of an ordinary function starts with a function name only where as an inline
function definition must start with the keyword inline before its name.
ii. For an ordinary function, the complier jumps from function call statement to another location
to execute the function and then back to the calling program. But with inline function, the compiler
replace the function call statement with function code itself and hence does not have to jump to another
location for execution.
Q. State the difference between a macro and an inline function?
Ans: i. Inline follows strict parameter type checking, macros do not.
ii. Macros are expanded by the preprocessor, while inline functions are parsed by the compiler.
Q. State some situation where inline function does not work?
Ans: i. For functions having a loop or a switch or a goto.
ii. For functions not returning values, if return statement exists.
iii. If function contain static variables or if the function is recursive.
Q. What is an abstract class?
Ans: Abstract class is a class that defines an interface, but doesn’t necessarily provide implementation
for all its member function. No object of an abstract class exists i.e., it cannot be instantiated.
Q. What is a concrete class?
Ans: A concrete class is a derived class of an abstract class that implements all the functionality. An
concrete class can be initiated i.e., its object can be created.
Q. State the difference between an abstract class and a concrete class?
Ans: Abstract Class Concrete Class
i. No object can be created i. Objects can be created
ii. Member functions defines interface ii. Member functions defines implementation
iii. Acts only as base class iii. Can be derived as well as base class
Q. What is a nested class?
Ans: A class declared within another class is called a nested class. The outer class is known as enclos-
ing class and the inner class is known as nested class.
Q. Explain the class scope?
Ans: Class Scope Description
i. Global Global Globally available to all the functions within a program and
hence the object of it can be created from any function within
the program

Page 3
ii. Local Local Locally available to the functions in which the class definition
occurs and hence the object of it can be created only within
the function that defines the class.
Q. Explain the class members scope?
Ans: Members Scope Description
i. Private Class Scope Members can be accessed only by the member functions or
friends of the class, not directly by an object.
ii. Protected Class Scope Members can be accessed only by the member functions or
friends of the class, not directly by an object.
iii. Public May be Global The scope depends on the referencing object. If the referenc
or Local ing object is local, the scope is local and if global then scope
is global.
Q. What is an object?
Ans: An object is an identifiable entity of a class with some characteristics and behavior i.e., data and its
associated functions. In other words, an object is a variable of a user-defined data type called a class.
Q. Illustrate the relationship of a class and its object with an example?
Ans: A class represents a group of similar object while an object is an instance of the class. In C++, a
class is a user-defined data type having data members and associated functions and an object is the
variable of the class through which public members can be accessed.
e.g. class CAR{
private:
//data members and member functions;
public:
//data members and member functions;
protected:
//data members and member functions;
}maruti,alto;
Here, maruti and alto are the objects of the class CAR and can access only the public section.
Q. Differentiate a class and an object?
Ans: Class Object
a) It is a user defined data type with data a) It is an instance of a class.
members and associated functions.
b) A class defines object. b) An object can't define a class.
c) A class has only the logical existence. c) An object has a physical existence.
d) A class is a static entity. d) An object is a dynamic entity.
Q. How are objects implemented in C++?
Ans: The object is implemented in C++ as follows:
i. Characteristics/attributes are implemented through member variables or data items of the
object.
ii. Behaviour is implemented through member functions called methods.
iii. It is given a unique name to give its identity.
Q. How memory is allocated for class and its objects?
Ans: When a class is defined, memory is allocated for its member functions only not for data mem-
bers. When an object is created, separate memory space is allocated for its data members. All objects
work with one copy of member function shared by all.
Page 4
Q. What is a static class members?
Ans: Static data member are the class members, having a prefix of the keyword static, that are com-
mon to all the objects of a class. Only one copy of it is maintained which is shared by all the objects of
the class.They are visible only within the class but their lifetime is entire program. They can be accessed
by all the member functions.
A member function that accesses only static data members of a class is static member function. It
cannot access any other data members except the static one.
Q. What is the significance of static data members?
Ans: The static data members are useful when some data values are to be shared across objects of the
same class.
Q. State the characteristics/properties of a static data member?
Ans: i. Only one copy of the static data member is created and is shared by all objects.
ii. It must be created and initialized before main() function.
iii. By default, static data member is initialized to zero when the first object of its class is created.
Q. State the characteristics/properties of a static member function?
Ans: i. A static function can access and manipulate only static data member of the class.
ii. A static member function is independent of any object and hence can be called using class
name instead of class object.
Q. Differentiate between static data member and ordinary data member?
Ans: i. Unlike ordinary data member, there is only one copy of static data member maintained for the
entire class which is shared by all the objects of that class.
ii. Though the static data member is visible only within the class as like ordinary data member,
but its lifetime is the entire program.
Q. Differentiate between a static member function and a ordinary member function?
Ans: i. Unlike ordinary member function, a static member function can access only static data mem-
bers.
ii. if accessible, a normal ordinary function is invoked with the help of an object where as, a static
member function with the class name.
Q. What is a friend function? Illustrate with an example?
Ans: A friend function is one which, though not being a member fucntion, has the full access rights to
the private member of a class.
e.g. class STD{
int a,b,res;
public:
void getdata() { cin>>a>>b; };
friend void showdata(STD r){ cout<<r.res; };//private data can be accessed
};
void main(){
STD rec;
rec.getdata(); //being a member function, access is done with an object
showdata(rec); //being a friend function, no need of object to access
}
Q. What is its significance of a friend function?
Ans: In OOPs concept, data members declared as private in a class are restricted from access by a
nonmember function. A friend function overcomes this, and has the ability to access the private member
of a class even not being a member function.
Page 5
Q. State the characteristic of a friend function?
Ans: i. They are not in the scope of a class and hence can be called from outside without an object of
class.
ii. They cannot access member function directly rather makes the use of objects of that class.
iii. They may be declared in any part(private or public) of the class.
Q. State the difference between friend function and a member function?
Ans: Friend function Member function
i. Has a global scope i. Has a class scope
ii. No object is needed to call ii. Needs an object to class
iii. Cannot access member function iii. Can access other member function
iv. Can be accessed from outside iv. Only public section can be accessed from outside.

BASE CONCEPT OF A CLASS

class STUDENT{
private:
int roll;
char sname[20];
float marks[5],percentage; Data Hiding
protected:
Encapsulation
float total;
void calc_perc();
public:
void get_data(); Data Abstraction
void show_data();
};
STUDENT std1,std2; Object(s) is also termed as an instance of a class and hence creating
or, STUDENT std[100]; or declaring object(s) of a class is termed as instantiation
Defining the member function outside class as
<return-type> Classname :: member_function(){
//function body
}
e.g. void STUDENT::calc_perc(){
//function body
}
General Terms
i. STUDENT - classname or tagname
ii. private, protected, public - access specifier
iii. roll, sname, marks, percentage, total - data members
iv. calc_perc(), get_data(), show_data() - member functions

Page 6

Você também pode gostar