OOP using C++ B.C.A 18-21 Sem 2 Second Internal March 2019
1.
We can define class members static using static
keyword. When we declare a member of a class as static it means no matter how
many objects of the class are created, there is only one copy of the static
member.
A static member is shared by all
objects of the class. All static data is initialized to zero when the first
object is created, if no other initialization is present. We can't put it in
the class definition but it can be initialized outside the class as done in the
following example by redeclaring the static variable, using the scope
resolution operator:: to identify which class it belongs to.
2.
A constructor is a special type of member
function that initialises an object automatically when it is created. Compiler identifies a given
member function is a constructor by its name and the return type. Constructor
has the same name as that of the class and it does not have any return type.
3.
Private, public and protected.
Visibility mode is used in the inheritance
of C++ to show or relate how base classes are viewed with respect to derived
class. When one class gets inherited from another, visibility mode is used to
inherit all the public and protected members of the base class.
4. Definition
OOP stands for Object-oriented programming
and is a programming approach that focuses on data rather than the algorithm,
whereas POP, short for Procedure-oriented programming, focuses on procedural
abstractions.
Programs
In OOP, the program is divided into small
chunks called objects which are instances of classes, whereas in POP, the main
program is divided into small parts based on the functions.
Accessing Mode
Three accessing modes are used in OOP to
access attributes or functions – ‘Private’, ‘Public’, and ‘Protected’. In POP,
on the other hand, no such accessing mode is required to access attributes or
functions of a particular program.
Focus
The main focus is on the data associated
with the program in case of OOP while POP relies on functions or algorithms of
the program.
Execution
In OOP, various functions can work
simultaneously while POP follows a systematic step-by-step approach to execute
methods and functions.
5.class classname
{
Access specifier:
Member variables;
Member functions();
};
6. A stream is a name given to a flow of
data at the lowest level. At the lowest level, data is just the binary data
without any notion of data type. Different streams are used to represent the
different kinds of data flow such as whether data is flowing into the memory or
out of the memory.
7. The inline functions are a C++
enhancement feature to increase the execution time of a program. Functions can
be instructed to compiler to make them inline so that compiler can replace
those function definition wherever those are being called.
8. A virtual function will become pure
virtual function when we append "=0" at the end of declaration of
virtual function. Pure virtual function doesn't have body or implementation. We
must implement all pure virtual functions in derived class. Pure virtual
function is also known as abstract function.
9. In C++, files are mainly dealt by using
three classes fstream, ifstream, ofstream available in fstream headerfile.
ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and
write from/to files.
Now the first step to open the particular
file for read or write operation. We can open file by
1. passing file name in constructor at the
time of object creation
2. using the open method
10. Encapsulation is defined as wrapping up
of data and information under a single unit. In Object Oriented Programming,
Encapsulation is defined as binding together the data and the functions that
manipulates them.
Abstraction means displaying only essential
information and hiding the details. Data abstraction refers to providing only
essential information about the data to the outside world, hiding the
background details or implementation.
11. A pointer is a variable whose value is
the address of another variable. Like any variable or constant, you must
declare a pointer before you can work with it. The general form of a pointer
variable declaration is −
type *var-name;
12. Symbolic Constants. A symbolic constant
is an "variable" whose value does not change during the entire
lifetime of the program...
13. Constructors are of three types:
Default Constructor.
Parametrized Constructor.
Copy COnstructor.
Explain each with example pgm.
14. Opening a File
A file must be opened before you can read from it or write to it. Either
ofstream or fstream object may be used to open a file for writing. And ifstream
object is used to open a file for reading purpose only.
Following is the standard syntax for open()
function, which is a member of fstream, ifstream, and ofstream objects.
void open(const char *filename,
ios::openmode mode);
Here, the first argument specifies the name
and location of the file to be opened and the second argument of the open()
member function defines the mode in which the file should be opened.
ios::app Append mode. All output to that
file to be appended to the end.
ios::ate Open a file for output and move
the read/write control to the end of the file.
ios::in
Open a file for reading.
ios::out
Open a file for writing.
ios::trunc
If the file already exists, its contents will be truncated before
opening the file.
15. Every object in C++ has access to its
own address through an important pointer called this pointer. The this pointer
is an implicit parameter to all member functions. Therefore, inside a member
function, this may be used to refer to the invoking object.
#include<iostream>
using namespace std;
/* local variable is same as a member's
name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The 'this' pointer is used to retrieve the object's x
// hidden by the local variable 'x'
this->x = x;
}
void print()
{
cout << "x = " << x
<< endl;
}
};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
16. 1)Only built-in operators can be
overloaded. New operators can not be created.
2) Arity of the operators cannot be
changed.
3) Precedence and associativity of the
operators cannot be changed.
4) Overloaded operators cannot have default
arguments except the function call operator () which can have default
arguments.
5) Operators cannot be overloaded for built
in types only. At least one operand must be used defined type.
6) Assignment (=), subscript ([]), function
call (“()”), and member selection (->) operators must be defined as member
functions
7) Except the operators specified in point
6, all other operators can be either member functions or a non member
functions.
8 ) Some operators like (assignment)=,
(address)& and comma (,) are by default overloaded.
17.
18.
Functions can be invoked in two ways: Call by Value or Call by
Reference. These two ways are generally differentiated by the type of values
passed to them as parameters.
The parameters passed to function are
called actual parameters whereas the parameters received by function are called
formal parameters.
Call By Value: In this parameter passing
method, values of actual parameters are copied to function’s formal parameters
and the two types of parameters are stored in different memory locations. So
any changes made inside functions are not reflected in actual parameters of
caller.
Call by Reference: Both the actual and
formal parameters refer to same locations, so any changes made inside the
function are actually reflected in actual parameters of caller.
Explain with example program.
19.
#include <iostream>
#include <string>
using namespace std;
class Student
{
string
name;
int
marks;
public:
void
getName()
{
getline(
cin, name );
}
void
getMarks()
{
cin
>> marks;
}
void
displayInfo()
{
cout
<< "Name : " << name << endl;
cout
<< "Marks : " << marks << endl;
}
};
int main()
{
Student
st[5];
for(
int i=0; i<5; i++ )
{
cout
<< "Student " << i + 1 << endl;
cout
<< "Enter name" << endl;
st[i].getName();
cout
<< "Enter marks" << endl;
st[i].getMarks();
}
for(
int i=0; i<5; i++ )
{
cout
<< "Student " << i + 1 <<”\n”l;
st[i].displayInfo();
}
return
0;
}
20.
An abstract class is a class that is
designed to be specifically used as a base class. An abstract class contains at
least one pure virtual function. You declare a pure virtual function by using a
pure specifier ( = 0 ) in the declaration of a virtual member function in the
class declaration.
21. There are two types of type conversion:
Implicit Type Conversion Also known as
'automatic type conversion'. Done by the compiler on its own, without any
external trigger from the user. ...
Explicit Type Conversion: This process is
also called type casting and it is user-defined.
Explain class to basic,basic to class,class
to class
22.
Inheritance in C++ The capability of a
class to derive properties and characteristics from another class is called
Inheritance. Inheritance is one of the most important feature of Object
Oriented Programming. Sub Class: The class that inherits properties from
another class is called Sub class or Derived Class.
Modes of Inheritance
Public mode: If we derive a sub class from
a public base class. Then the public member of the base class will become
public in the derived class and protected members of the base class will become
protected in derived class.
Protected mode: If we derive a sub class
from a Protected base class. Then both public member and protected members of
the base class will become protected in derived class.
Private mode: If we derive a sub class from
a Private base class. Then both public member and protected members of the base
class will become Private in derived class.
Explain single,multiple,multi level,hybride
and heirarchichal inheritance with example.
23. The word polymorphism means having many
forms. In simple words, we can define polymorphism as the ability of a message
to be displayed in more than one form.
Real life example of polymorphism, a person
at a same time can have different characteristic. Like a man at a same time is
a father, a husband, a employee. So a same person posses have different
behavior in different situations. This is called polymorphism.
Polymorphism is considered as one of the
important features of Object Oriented Programming.
In C++ polymorphism is mainly divided into
two types:
Compile time Polymorphism
Runtime Polymorphism
Compile time polymorphism: This type of polymorphism
is achieved by function overloading or operator overloading.
Operator overloading: C++ also provide
option to overload operators. For example, we can make the operator (‘+’) for
string class to concatenate two strings. We know that this is the addition
operator whose task is to add to operands. So a single operator ‘+’ when placed
between integer operands , adds them and when placed between string operands,
concatenates them.
Function overloading: Two or more functions
having same name but different argument(s) are known as overloaded functions.
Virtual function : A virtual function a member
function which is declared within base class and is re-defined (Overriden) by
derived class. When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for that object
and execute the derived class's version of the function.
Explain with example
24.
C++
OOPs Concepts
Object.
Class.
Inheritance.
Polymorphism.
Abstraction.
Encapsulation.
Explain
each with figures.
25. A function is a group of statements
that together perform a task. Every C++ program has at least one function,
which is main(), and all the most trivial programs can define additional
functions.
we can divide up our code into separate
functions. How you divide up your code among different functions is up to you,
but logically the division usually is such that each function performs a
specific task.
A function declaration tells the compiler
about a function's name, return type, and parameters. A function definition
provides the actual body of the function.
Explain function parts,static,friend,inline
functions etc with example.
Comments
Post a Comment