Professional Documents
Culture Documents
Derived data types: From fundamental types other types can be derived by using the declaration operators.
These are of 5 types:
1.Arrays.
2.Functions.
3.Pointers.
4.References.
5.Constants.
• switch statement
SECTION B
1. Objects
2. Classes
3. Abstraction
4. Encapsulation
5. Inheritance
6. Overloading
7. Exception Handling
Objects
Objects are the basic unit of OOP. They are instances of class, which have data members and uses
various member functions to perform tasks.
Class
It is similar to structures in C language. Class can also be defined as user defined data type but it also
contains functions in it. So, class is basically a blueprint for object. It declare & defines what data variables
the object will have and what operations can be performed on the class's object.
Abstraction
Abstraction refers to showing only the essential features of the application and hiding the details. In C++,
classes can provide methods to the outside world to access & use the data variables, keeping the
variables hidden from direct access, or classes can even declare everything accessible to everyone, or
maybe just to the classes inheriting it. This can be done using access specifiers.
Encapsulation
It can also be said data binding. Encapsulation is all about binding the data variables and functions
together in class.
Inheritance
Inheritance is a way to reuse once written code again and again. The class which is inherited is called
the Base class & the class which inherits is called the Derived class. They are also called parent and
child class.
So when, a derived class inherits a base class, the derived class can use all the functions which are
defined in base class, hence making code reusable.
Polymorphism
It is a feature, which lets us create functions with same name but different arguments, which will perform
different actions. That means, functions with same name, but functioning in different ways. Or, it also
allows us to redefine a function to provide it with a completely new definition
Q2. What is Compiler? Discuss the various C++ compilers
Ans.
A compiler is a special program that processes statements written in a particular programming language and turns them
into machine language or "code" that a computer's processor uses. Typically, a programmer writes language statements
in a language such as Pascal or C one line at a time using an editor.
An array is a collection of a fixed number of values of a single type. For example: if you want to store
100 integers in sequence, you can create an array for it.
int data[100];
The size and type of arrays cannot be changed after its declaration.
1. One-dimensional arrays
2. Multidimensional arrays
In the real world, you often have many objects of the same kind.
In object-oriented software, it's also possible to have many objects of the same kind that share
characteristics: rectangles, employee records, video clips, and so on. Like the bicycle
manufacturers, you can take advantage of the fact that objects of the same kind are similar and you
can create a blueprint for those objects. A software blueprint for objects is called aclass .
Method
A method is a member function of a class, but in C++ they are more commonly called member functions
than methods (some programmers coming from other languages like Java call them methods). A function is
usually meant to mean a free-function, which is not the member of a class
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the
name of the pointer variable.
Q4. What are constructor and copy constructor?
Ans.
A constructor is a special method of a class or structure in object-oriented programming that initializes an
object of that type. A constructor is an instance method that usually has the same name as the class, and can
be used to set the values of the members of an object, either to default or to user-defined values.
n C++, we can make operators to work for user defined classes. This means C++ has the ability to provide the
operators with a special meaning for a data type, this ability is known as operator overloading.
For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by
just using +.
Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional
Number, Big Integer, etc.
SECTION 2
Q1.
(a) Write a program uses a nested for loop to find the prime numbers from 2 to 100.
(b) Define the do-while loop statements.
Ans.
a)
int i,j,s=0,d=0;
for(i=1;i<100;i++)
s=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
s=s+1;
if(s==2)
Cout<<i<<endl;
d=d+1;
b)
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed
to execute at least one time.
Syntax
The syntax of a do...while loop in C++ is −
do {
statement(s);
}
while( condition );
Q2.
What are the difference forms of inheritance? Explains with an example program.
Ans.
C++ supports six types of inheritance as follows:
• Single Inheritance
• Multilevel Inheritance
• Multiple Inheritance
• Heirarchical Inheritance
• Hybrid Inheritance
• Multipath Inheritance
Single Inheritance
A derived class with only one base class is called single inheritance.
Multilevel Inheritance
A derived class with one base class and that base class is a derived class of another is
called multilevel inheritance.
Multiple Inheritance
A derived class with multiple base class is called multiple inheritance.
Heirarchical Inheritance
Multiple derived classes with same base class is called hierarchical inheritance.
Hybrid Inheritance
Combination of multiple and hierarchical inheritance is called hybrid inheritance.
Multipath Inheritance
A derived class with two base classes and these two base classes have one common base class is
called multipath inheritance.More info.
Example
Class one
{
Void displayone()
{
Cout<<”This Is display of class one”<<endl;
}
}
Class two : public one
{
Void displaytwo()
{
Cout<<”this is display function of class two”<<endl;
}
}
Void main()
{
Two k;
k.displayone();
k.displaytwo();
}