0% found this document useful (0 votes)
4 views9 pages

ch3

The document provides an overview of key concepts in C++ and Object-Oriented Programming (OOP), including features of OOP, classes and objects, member functions, data abstraction and encapsulation, constructors and destructors, operator overloading, and inheritance. It explains various data types, pointers, string functions, static members, friend functions, and polymorphism. Additionally, it includes examples and rules for implementing these concepts in C++.

Uploaded by

mahllenilesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
4 views9 pages

ch3

The document provides an overview of key concepts in C++ and Object-Oriented Programming (OOP), including features of OOP, classes and objects, member functions, data abstraction and encapsulation, constructors and destructors, operator overloading, and inheritance. It explains various data types, pointers, string functions, static members, friend functions, and polymorphism. Additionally, it includes examples and rules for implementing these concepts in C++.

Uploaded by

mahllenilesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 9

QUESTIONS FROM CH 3 C++

1. What are the features of C++/ OOP ?


Ans: The features of OOP are as follows:
1. It follows bottom up approach.
2. Emphasis is on data rather than procedure.
3. Programs are divided into objects.
4. Every object has its own data and function.
5. Data of object can be accessed by the function associated with that object.
6. Data is hidden and can’t be accessed by external functions.
7. Objects communicate with one another through function.
8. New data can added by creating an object whenever needed.

2. What is Class and Object? Explain with example.


Ans: Class:
1. Class is a User Defined data type in C++.
2. Class is an encapsulation that provides security to the data.
3. Class has Data Members and Member functions in it. These can be either public or private by
default.
4. Once the class is created ,we can create any number of copies called as objects
Objects:
1. Objects is variable of type class
2. The data member and member functions of class can be accessed only by the object of same
class.
Example: class student
{public:
Int roll;
float percent;
void get()
{ cin>>roll>>percent; }
};
void main()
{
class student s1,s2,s3;
s1.get();
getch();
}
Here student is a class and s1,s2 and s3 are objects of class student
3. What are members of class? How to define member functions inside the class and outside the
class? Give example.
Ans:
1. Class is a user defined data type in C++.Class contains Data and Function associated with
it.These data and function are called members of class.
2. The member functions of class are always declared inside the class. But it can be defined either
inside the class or outside the class.
3. While defining the member function inside the class, the declaration and definition is done at
the same time. Normally if function definition is small then it is defined inside the class
The syntax of function definition inside the class is :
Return type name of function (argument list)
4. While defining function outside the class, declaration is done inside the class and definition is
written outside the class.
Syntax for function definition outside class is as follows:
Return type class Name scope resolution operator function name (argument list)
5. Example :
class student
{int roll;
float percent;
void get() //Function definition inside the class
{ cin>>roll>>percent; }
void put( );
};
void student : : put() //Function definition outside theclass
{
cout<<”roll number ”<<roll
cout<<”\npercentage is ”<<percent;
}
6. What is data abstraction and data encapsulation?
Ans: Data Abstraction: Class is a way to bind data and its associated function together. it allows data to
be hidden from outside functions. When defining a class, we are creating a new abstract data type like
other basic data types. Collection of data and function in class is called as Data Abstraction.
Here Abstraction refers to providing only essential information to the outside world and hiding their
background detail i.e. represent needed information in program without representing detail.
Data Encapsulation: Encapsulation is an OOP concept thai binds data and function that manipulates the
data. And keep both safe from outside interference and misuse.Data encapsulation led to concept of
data hiding through creation of user defined data type called as class. The member of class can be either
private or public or protected. The private members of class are hidden from outside world ,this is
called as data hiding ,it forms a capsule of data and function. This is called as data encapsulation.

7. What is a constructor and destructor function? Explain in detail with example.


Ans: (you can write any six points among these )
1. Constructor is a special type of function whose name is same as class name
2. It is specially designed to initialize default values to an object
3. Constructor need not be called. It is automatically called when the object is created.
4. We can also give an explicit call to constructor function.
5. Constructor is always public
6. Constructor never returns any value
7. It can be defined inside or outside the class.
8. It may have arguments as either value or a complete object. (parameterized constructor)
9. Constructor can be overloaded.
Destructor:
1. Name of destructor function is always same as class name but it is preceded by ~ tidal symbol.
2. Destructor function is specially designed to free the space allocated to object.
3. Destructor is called automatically when an instance of class is closed ( i.e. } closing curly
beacket)
4. Destructor can be explicitly called if needed.
5. Destructor never has any argument.
6. Destructor never returns any value.
7. Destructor is always public.
Example:
class circle
{
float rad,pi,area;
circle() //Constructor
{pi=3.14; rad=10;}
~circle(); //Destructor
};

8. What are rules for writing a constructor and destructor function?


Ans: Rules for Writing a constructor are as follows:
1. 1 Constructor name must be same as class name.
2. Constructor is declared with no return value.
3. It may not be a static function or a virtual function.
4. It should be public.
Rules for writing a destructor function are as follows:
1. A destructor function name is same as class name .only the preceeding character is ~ in name.
2. Destructor cant return any value.thus declared with no return type
3. Destructor takes no argument.
4. Destructor must be declared in public section

9. What is function overloading? Explain with example.


Ans:
1. When we declare multiple functions of the same name ,then that function is said to be
overloaded function
2. These function overloading means the use of samr function name to createfunctions that
performs variety of different tasks.
3. Using function overloading we can design a family of functions with one function name but with
different argument list.
4. The function would perform different operations depending on argument list in function call.
5. The correct function to be invoked is determined by checking the number of argumentand type
of argument but not the function type.
Example :
Here add function is overloaded
int add()
Int add( int a ,int b)
Int add ( float x ,float y)
Int add( int p ,float q)

10. What is operator function? How to overload operator function?


OR
What is Operator Overloading? Explain with example.
Ans:
1. A specially designed function ,whose name is a valid operator in C++, is called as operator
function. example: void operator +(int a,int b)
2. Operator overloading is one of the feature of C++, that allows to use all operator with objects
also. For instance we can add two objects, compare two objects etc.this is not possible without
operator overloading.
3. In operator overloading we write a complete definition of that operator . That can extend the
meaning of operator. Extending the semantics of operator is called as Operator overloading.
4. The actual meaning of operator is not changed , only its scope is extended.
Eg.

11. Which operators cannot be overloaded? What is the difference between Binary operator
overloading erloading and unary operator overloading?
Ans : Some operator cannot be overloaded these are as follows:
1. Size of operator
2. Membership operator . (dot)
3. Pointer to member operator .* (dot and asterisk)
4. Scope resolution operator : :
5. Conditional Operator ? :
Difference between Binary and Unary operators:
1. Unary operators overloaded by member function do not returns any value.
2. Unary operators overloaded by member function do not take any argument.
3. If it is friend function, it takes only one argument.

1. Binary operators overloaded by member function returns a value .It generally return an object
2. Binary operators overloaded by member function take argument it may be an object.
3. If it is friend function, it takes two arguments.

12. What are rules for overloading operators?


Ans: Rules for operator overloading are:
1. Only existing operators can be overloaded. New operators can’t be created.
2. Overloaded operator must have atleast one oprand that is user defined type(one oprand must
be object)
3. We cant change basic meaning of operator.
4. Overloaded operator follow the same syntax as the original operator.
5. Some operators cant be overloaded .
6. Unary operator never returns any value.
7. Unary operator donot take any argument.
8. Binary operator has object as an argument.
9. Binary operator returns an object

13. What are different data types in C++?


Ans: There are three data types in C++
1. User Defined data type: These are designed i.e. created by user eg
CLASS,STRUCTURE,UNION,ENUM
2. Built in data types: These Are basic data types in c++.These are inbuilt in c++ eg.
INT,FLOAT,CHAR,VOID,DOUBLE
3. Derived data types : These are formed from the basic data types.eg. ARRAY,POINTER,FUNCTION

14. What is a pointer?How to use it for Call by value and Call by reference? Explain.
Ans: Pointer is a variable that holds address of some other variable. Pointer variable differs from simple
variable with “* “ in front of variable eg . int *ptr is a pointer variable of type integer. These pointers
can be used instead of simple variables. While using pointer we handle two operators “ * ” and ” & ”.
These pointers can be used with functions also. Pointers can be used as an argument or these can be
used as return variables also.
In function call if simple variables are used as argument ,it is called as call by value.
In function call if pointer variables are used as argument ,it is called as call by reference.
eg
15. What are different string functions?
Ans: In C++ different string functions are used to handle strings i.e. bulky text data.
Some of the string functions are as follows:
1. strlen(): This function finds length of argument included in brackets .The argument may be a
string or it can be a variableof string type. It returns an integer number ,which is one more than total
number of characters in that string (null character occupies one character).
Eg. X=strlen(“shree”)
This makes x= 6

2. strcpy(): This function copies data from source into destination that are included as arguments in
brackets .If the source contents some data, it will be overwritten.The source argument may be a
string( spelling ) or it can be a variable of string type and destination argument is always a string type
variable. It do not returns anything.
Eg strcpy(name, “shree”)
This makes name=”shree”.

3. strcat():This function concatenates i.e. mixes two strings. The contents of source are added into
destination argument included in brackets .The source argument may be a string or it can be a
variableof string type, but the destination is always a string type variable. It never returns any value.
The length of destination increases.
Eg.strcat(name,”deshmukh”)
Will make name=”shree deshmukh”

4. strcmp() : This function compares two strings.and it returns theASCII difference between first
uncommen character in two strings. If both strings are equal,then it returns 0.
Eg int x= strcmp(“swati”,”swami”)
Will make x=8.the ASCII difference between “m”and “t”.

16. What are the static members of class?


Ans:
1. Static members of class are special kind of members of a class. These may be static data
members or static member function of that class.
2. Static member are not separately allotted for each object. only one copy of static member is
shared by all objects.
3. Static variable can be handled by static functions only.
4. Static data members are created to count occurrence of total objects in any program.
5. Members can be made static by simply adding a keyword static before declaring that
member. Eg. Static int A;
6. Static
17. What is a friend function of class?
Ans:When we have to use same functions in many classes, we can declare function in many classes and
define it for only one class. We donot define it in all classes then also the same definition can be used in
all classes if the function is declared as friend function in all classes.We can declare it as friend function
simply by putting a keyword friend at the time of declaration in all classes.
Eg

class one
{public:
friend void welcome() //welcome() defined in class one
{cout<<”wecolme to c++ program”;}

};
class two
{public:
friend void welcome(); //welcome declared as friend function in class two
};
class three
{
friend void welcome(); //welcome declare as friend function in class three
};
void main()
{
three ob1;
two ob2;
one ob3;
ob3.welcome(); //welcome() is defined only for class one still can be used by object of two three
ob2.welcome();
ob1.welcome();
}
18. What is inheritance? What are its different types? Explain any one
Ans : Inheritance:
1. Inheritance is one of the feature of C++ that allows to use member of one class in another class.
2. The original class is called as base class.
3. The class in which member of base class are used is called as derived class.
4. Only the public member of base class can be used in derived class. Private members cant be
inherited.
5. The public member from base class can be made either private or public in the derived class.
6. The general syntax of class derivation is:
Class derived class name : visibility lable base classname

7. There are five types of Inheritance:


a. single inheritance .
b. Multiple inheritance.
c. Multilevel inheritance.
d. Hierarchical inheritance.
e. Hybrid inheritance.
Multilevel inheritance : In multilevel
inheritance there is a base class, a class
is derived from base class and from the
derived class one more class is derived .
Deriving one class from the derived
class is called as multilevel inheritance.
As shown in the diagram.
Example for multilevel inheritance:
#include<iostream.h>
class one
{
public:
Int a;
};
class two: public one
{
public:
int b;
};
class three: public two
{
public:
Int sum;
void find()
{ cin>>a>>b;
sum=a+b;
cout<<sum;
}
};
void main()
{class three t;
t.find();
getch();
}

19. What is Polymorphism? What is run time polymorphism and compile time polymorphism?
Ans: C++ Polymorphism means that a call to member function will cause a different function to be
executed, depending on the type of argument or object that invokes the function.
Sometime the function call is fixed (which function is to be called)before execution of program i.e. at the
compile time itself.this is also called as early binding or compile time polymorphism.

You might also like