Sie sind auf Seite 1von 10

UNIT II 2 MARK QUESTIONS CLASSES AND OBJECTS 1. What is a class?

? The objects with the same data structure(attributes) and behavior(operation) are grouped into class. All those objects possessing similar properties are grouped with same unit. A class is a new way of creating and implementing a user defined data type. Classes provide a method for packing together data of different types. A class is a way to bind the data and its associated function together. Synta ! class classname " ## body of a class $% where class is the keyword and classname is the name of the class. 2. What is a !"#$ct? The process of creating variables of a class ie creating an object is called instantiation. That actually creates objects in the program by setting aride memory space for its storage. &bject is defined as the instance of a class which represents the real world entity. %. What a&$ th$ '$at(&$s !' class? '. The keyword class specifies user defined data type class name. (. The body of the class is enclosed within braces and is terminated by a semicolon. ). The class body contains the declaration of variables and function. *. The class body has three access specifiers+ (i) private (ii) public (iii) protected. ). What a&$ *$*"$& '( cti! s a + +ata *$*"$&s? The variables declared inside a class are known as data members. The function which are declared inside a class are known as member functions. ,. What a&$ acc$ss s-$ci'i$&? Access specifiers are said to be visibility labels. The three access specifiers are .i/0&i1at$! Accessible by only its own members and certain special function called friend functions. 0&!t$ct$+! Accessible by members of inherited class 0("lic! Access allowed by other members in addition to class members and objects.

2. 3!4 ca 4$ acc$ss class *$*"$&? After creating an object we can access the class members using dot(.) operator. The synta for accessing members of a class is objectname.data member objectname.function name(Actual Arguments) 5. What a&$ th$ 4a6s '!& s-$ci'6i 7 a *$*"$& '( cti! ? ') ,ember function can be -nside the class body . The member function can be declared and defined within the class () ,ember function can be &utside the class body. . The member function is declared within the class and defined outside the class using scope resolution operator. T! s-$ci'6 a *$*"$& '( cti! i si+$ th$ class "!+6 class classname " private! ........ public! function name() " $ $% T! s-$ci'6 a *$*"$& '( cti! !(tsi+$ th$ class "!+6 class classname " ..... public! returntype ,ember funtion(arguments)% $% returntype classname !! member function(arguments) " ##body of the function $ 8. 3!4 is +ata hi+i 7 achi$1$+ "6 a class? The data member and member function declared under private are not accessible even by mistake by members outside the class. This is referred as data hiding. 9. W&it$ th$ 1isi"ilit6 !' class *$*"$&. Access Accessible to &wn specifier class member /rivate yes /rotected yes /ublic yes &bjects of a class no no yes

1:. What is a $*-t6 class? -t is possible to have a class that has neither data nor code. The classes which doesn0t contain any data member or member function -t is called an empty class or stubs. Class student " $% 11. What a&$ th$ 4a6s !' +$cla&i 7 a !"#$cts? There are two ways of declaring an object. (i) &bject can be created by placing their names immediately after the closing braces of class declaration (ii) &bject can be created inside the main function which is prefi ed by the class name Class student " ..... ...... $s'% class student " ..... ..... $% main() " student s'% ..... $

12. What a&$ th$ 4a6s t! -ass a !"#$ct as a&7(*$ t? ')0ass;"6;1al($! a copy of the entire object is passed to the function. 2/0ass;"6;&$'$&$ c$! only the address of the object is passed e plicitly to the function. 1%. What is '&i$ + '( cti! a + '&i$ + class? A friend function and a friend class can be able to access the private members of a class. The function or class declaration must be prefi ed by the keyword friend. 1). What a&$ th$ s-$cial cha&act$&istics !' *$*"$& '( cti! ? (i) Several different classes can use the same function name. The membership label will resolve their scope. (ii) ,ember function can access the private data of a class. An non member function cannot do so. (iii) The return type of a member function can be of object data type. (iv) The member function can receive arguments. &bjects can also be passed as arguments. (v) ,ember function can call another member function directly+ without using dot operator. This is called as nesting of member function.

CONSTRUCTOR < DESTRUCTOR 1,.What is c! st&(ct!&? . A constructor is a special member function whose main operation is to allocate the re1uired resources such as memory and initiali2e the objects of its class. . The constructor is distinct from other member function of its class and it has the same name as its class. . -t is e ecuted automatically when a class is instantiated. . The constructor has no return value specification. 12. W&it$ th$ s6 ta= !' th$ c! st&(ct!&. class classname " private! public! classname()% $ classname !!classname() " ##constructor body definition $ 15. W&it$ th$ cha&act$&istics !' a c! st&(ct!&? '. -t has the same name as that of the class to which it belongs. (. -t is e ecuted automatically when a class is instantiated. ). The constructor has no return value specification. *. -t is to used to allocate the re1uired resources such as memory to the dynamic data members of a class 3. -t is used to initiali2e the objects of its class. 18. W&it$ a s6 ta= '!& -a&a*$t$&i>$+ c! st&(ct!&. class classname " private! public! classname(arg'+arg(+....argn) " $ $% 19. What is +$'a(lt c! st&(ct!&? The default constructor is a constructor+ which does not have any arguments. -t is invoked when the object is instantiated with no arguments. -t follows that each class can have only one default constructor.

2:.What is c! st&(ct!& !1$&l!a+i 7? An interesting feature of the constructors is that a class can have multiple constructors. This is called constructor overloading. All constructors have the same name as the class name+ but they should differ either by the number of arguments or the datatype of the arguments. 21. What a&$ th$ +i''$&$ c$s "$t4$$ c! st&(ct!& a + +$st&(ct!&. S.NO CONSTRUCTOR DESTRUCTOR '. -t has the same name as that of the -t also has the same name as the class to which it belongs. class name but prefi ed by tilde(4) (. -t is e ecuted automatically -t is invoked automatically whenever the class is instantiated. whenever an object goes out of scope. ). -t does not have any return type and -t has neither arguments nor a may or may not have arguments return value. *. ,any constructors may present in There can be only one the class destructor in each class 3. Constructors cannot be virtual 5estructors can be virtual 6. Constructors can be overloaded. 5estructors cannot be Constructor with arguments can co. overloaded e ist with constructors without arguments. 22. What is +6 a*ic c! st&(cti! a + +6 a*ic +$st&(cti! ? Allocation of memory to objects at the time of their construction is known as dynamic construction. The allocated memory can be released when the object is no longer needed (goes out of scope) at runtime is known as dynamic destruction. 2%. What is c!-6 c! st&(ct!&? A constructor having a reference to an instance of its own class as an argument is known as copy constructor. A copy constructor copies the data members and member functions from one object to another 2). What a&$ +$st&(ct!&s? 7hen an object is no longer needed it can destroyed. A class can have another special member function called the destructor+ which is invoked when an object is destroyed. 2,. Wh$ +!$s th$ +$st&(ct!& 7$t call$+ '!& a l!cal ! ;static 1a&ia"l$ a + '!& a 7l!"al !& static 1a&ia"l$? ?!& a l!cal ! ;static 1a&ia"l$@ 8or objects which are local non.static variables+ the destructor is called when the function in which the object is defined is about to terminate. ?!& a 7l!"al !& static 1a&ia"l$@

8or global or static variable+ the destructor is called before the program terminates. 9ven when the program is interrupted using an e it() call+ destructors are called for all objects that e ist at that time. O0ERATOR OAERLOADINB 22. D$'i $ !-$&at!& !1$&l!a+i 7. &perator overloading is a concept that allows the user to give additional meaning to most operators. So that+ it can be used with the user0s own datatypes+ thereby making the datatypes easier to use. This feature of C:: is one of the methods of realising polymorphism. 25. What a&$ th$ a--licati! s !' !-$&at!& !1$&l!a+i 7 c! c$-t? &perator &verloading concepts are applied to the following two principal areas. (i) 9 tending the capability of operators to operate on user defined data. (ii) 5ata conversion. 28. What is ( a&6 !-$&at!& !1$&l!a+i 7? &verloading without e plicit arguments to an operator function is known as unary operator overloading. Synta ! ;eturntype operator operator symbol() " ##body of operator funtion $ 29. D$'i $ !-$&at!& C$64!&+. The keyword operator facilitates overloading of C:: operators. The keyword <operator0 indicates that the operator symbol following it is a C:: operator+ to be overloaded to operate on members of its class. %:. What a&$ th$ st$-s i 1!l1$+ i !-$&at!& !1$&l!a+i 7 -&!c$ss? The process of operator overloading involves the following steps (i) 5eclare a class+ whose objects are to be manipulated using operators. (ii) 5eclare the operator function+ in the public part of the class. -t can be either a normal member function (or) a friend function. (iii) 5efine the operator function either within the body of a class or outside the body of a class. %1.W&it$ th$ s6 ta= '!& i 1!Ci 7 ( a&6 !-$&at!& '( cti! . The synta for invoking unary operator function is given as &bject operand or &perator object The first synta can be used to invoke a prefi operator function. 8or eg. ::id ' The synta can be used to invoke a postfi operator function. 8or eg. -d ':: %2. What is th$ s6 ta= '!& "i a&6 !-$&at!& !1$&l!a+i 7? The synta for binary operator overloading is given below

;eturntype operator operatorsymbol(argument) " ##body of operator function $ ;eturn type may be primitive+ void or user defined. %%. 3!4 th$ a&7(*$ ts ca "$ -ass$+ i "i a&6 !-$&at!& !1$&l!a+i 7? The binary overloaded operator function takes the first object as an implicit operand and second operand must be passed e plicitly. The data members of the first object are accessed without using dot operator where as the second argument members can be accessed using the dot operator if the argument is an object+ otherwise it can be accessed directly. %). What a&$ &(l$s '!& !1$&l!a+i 7 !-$&at!&s? (i) &nly e isting operators can be overloaded. =ew operators cannot be created. (ii) &verloaded operators follow the synta rules of the original operators. They cannot be overridden. (iii) 7e cannot change the basic meaning of an operator. (iv) There are some operators that cannot be overloaded. 9g. Si2eof !! >! si2eof operator scope resolution operator conditional operator

%,. What is th$ '( cti! !' c! cat$ ati! !' st&i 7s? -t concatenate two strings resulting in a single string concatenation of strings which is performed by using the library function strcat() e plicitly. IN3ERITANCE %2. D$'i $ I h$&ita c$? -nheritance is a techni1ue of organi2ing information in a hierarchical form. -t is like a child inheriting the features of its parents. -t can also be defined as the techni1ue of building new classes (derived classes) from the e isting classes (base classes) is called inheritance. class derivedclassname ! ?visibilty mode@ baseclassname " $% %5. What is *$a t "6 l$1$l !' i h$&ita c$? -t refers to length of path from its root. 5epending on this level of inheritance+ there are different forms of inheritance like single+ multiple+ multilevel+ hierarchical+ hybrid and multipath inheritance %8. What is a class hi$&a&ch6 !& i h$&ita c$ t&$$? The pictorial representation of inheritance showing the inter relationship among the classes involved is known as class hierarchy.

%9. What a&$ th$ 1a&i!(s '!&*s !' i h$&ita c$? Single 5erivation of a class from only one base class ,ultiple 5erivation of a class from several base class Aierarchical 5erivation of several class from a single base class (or) one class can be inherited from more than one base class
C

,ultilevel 5erivation of a class from another derived class


A B

Aybrid 5erivation of a class involving more than one form of inheritance


A B C

,ultipath 5erivation of a class from other derived class+ which are derived from same base class
A B C

):. What a&$ th$ "$ $'its !' i h$&ita c$? '. Code reusability (. 9ase of code maintenance and e tension ). ;eduction in processing time *. Code sharing AIRTUAL ?UNCTIONS )1. What a&$ call$+ 1i&t(al '( cti! s? /olymorphism indicates the form of a member function that can be changed at runtime. Such member functions are called 1i&t(al '( cti! s and the corresponding class is called -!l6*!&-hic class. So it allows postponing the decision of selecting the suitable member functions until runtime. class classname " public! vitual returntype functionname(arguments) " $ $% )2. What a&$ Static a + +6 a*ic "i +i 7? A function call can be bound to the actual function either at compile time or at runtime. ;esolving a function call at compile time is known as c!*-il$;ti*$ "i +i 7 !& $a&l6 "i +i 7 !& static "i +i 7. ;esolving a function call at run time is known as &( ti*$ "i +i 7 !& lat$ "i +i 7 !& static "i +i 7.

)%. What a&$ -(&$ 1i&t(al '( cti! s? Those Birtual functions that are defined with a null.body and so it has no definition. -t is similar to a dummy.function in base class. -t is declared as a virtual function followed by CD. Such functions are pure virtual functions. 1i&t(al &$t(& t6-$ '( cti! a*$.a&7(*$ ts/D:E

)). What is th$ +i''$&$ c$ "$t4$$ -(&$ a"st&act class$s a + a"st&act class$s? A"st&act class An abstract class is a one that has no instances and not designed to create object. An abstract class is only designed to be inherited. -t specifies an interface at certain level of inheritance and provides framework+ upon which other classes can be built. 0(&$ A"st&act class$s A class containing pure virtual functions that cannot be used to define any objects of its own and such classes are called pure abstract classes or simply abstract classes All other classes without pure virtual functions which cannot be instantiated are called as concrete classes

),. What a&$ th$ -&!-$&ti$s !' -(&$ 1i&t(al '( cti! s? '. -t has no implementation in the base class (. -t cannot be instantiated. ). -t acts as an empty bucket that the derived class is supposed to fill. *. -t can be invoked by its derived class. )2. What a&$ th$ &(l$s '!& 1i&t(al '( cti! s? '. There must be a definition of virtual function in the base class (. They cannot be static members. ). They can be a friend function to another class *. They are accessed using object pointers. 3. To reali2e the potential benefits of virtual functions supporting runtime polymorphism+ they should be declared in the public section of a class. ?UNCTION OAERLOADINB )5. What is *$a t "6 '( cti! !1$&l!a+i 7? 8unction overloading is a concept that allows multiple functions to share the same name with different argument types. Assigning one or more function body to the same name is known as function overloading or function name overloading. 12 MARKS 1/Explain Function Overloading?

2)Expalin operator Overloading 3)Explain constructors with example. 4)Explain destructors with example 5)Explain method overriding 6) Explain overloading nar! " #inar! operator?

Das könnte Ihnen auch gefallen