© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

9. Differentiate between a template class and class template?

Ans:

Template class:
A generic definition or a parameterized class not instantiated until the client provides the needed information. It’s jargon for plain templates.
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. It’s jargon for plain classes.

Ex 1: Find the output of the following program
class base
        {
        public:
            void baseFun(){ cout<<"from base"<<ENDL;}
        };
 class deri:public base
        {
        public:
            void baseFun(){ cout<< "from derived"<<ENDL;}
        };
void SomeFunc(base *baseObj)
{
        baseObj->baseFun();
}
int main()
{
base baseObject;
SomeFunc(&baseObject);
deri deriObject;
SomeFunc(&deriObject);
}

Ans:
            from base
            from base

Explanation:

As we have seen in the previous case, SomeFunc expects a pointer to a base class. Since a pointer to a derived class object is passed, it treats the argument only as a base class pointer and the corresponding base function is called.

Ex 2: Find the output of the following program
class base
        {
        public:
            virtual void baseFun(){ cout<<"from base"<<ENDL;}
        };
 class deri:public base
        {
        public:
            void baseFun(){ cout<< "from derived"<<ENDL;}
        };
void SomeFunc(base *baseObj)
{
        baseObj->baseFun();
}
int main()
{
base baseObject;
SomeFunc(&baseObject);
deri deriObject;
SomeFunc(&deriObject);
}

Ans:
            from base
            from derived

Explanation:

Remember that baseFunc is a virtual function. That means that it supports run-time polymorphism. So the function corresponding to the derived class object is called.

SLogix Student Projects
bottom