0% found this document useful (0 votes)
159 views

Oop Notes

This document describes the basic structure of a C++ program. It discusses the following sections: 1. Include header files which define statements like cout and cin. 2. Class declaration where classes necessary for the program are declared. 3. Member function definitions where functions of a class are designed. 4. Main function where objects are created and functions are called.

Uploaded by

N Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views

Oop Notes

This document describes the basic structure of a C++ program. It discusses the following sections: 1. Include header files which define statements like cout and cin. 2. Class declaration where classes necessary for the program are declared. 3. Member function definitions where functions of a class are designed. 4. Main function where objects are created and functions are called.

Uploaded by

N Jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

General C++ program has following structure Description:- 1.

Include header files In this section a programmer include all header files which are require to execute given
program. The most important file is iostream.h header file. This file defines most of the C++statements like cout and cin. Without this file one cannot load C++ program. 2.
Class Declaration In this section a programmer declares all classes which are necessary for given program. The programmer uses general syntax of creating class. 3. Member
Functions Definition This section allows programmer to design member functions of a class. The programmer can have inside declaration of a function or outside declaration
of a function. 4. Main Function Program In this section programmer creates objects and calls various functions writer within various class.

Write any two characteristics of destructor. A destructors is a special member function that is automatically called when an object is destroyed . destructors are invoked by
the compiler implicity when the termination of the programme takes the places. But it is always better to invoke the destructor explicity by writing it into the program code.
Characteristics: 1. It is used to destroy objects created by a constructor. 2. Name of destructor and name of the class is same. 3. Its name is preceded with tilde (~) symbol. 4.
It never takes any argument. 5. It does not return any value. 6. It is invoked implicitly by the compiler upon exit from the program (or block or function) i.e when scope of
object is

CONSTRUCTOR : A constructor is a special member function which is used to allocate memory space and values to the data members of that objects.OVERCONSTRUCTOR: A
constructor is a special member function whose task is to initialize the objects of its class. It is special because its name is same as the class name. The constructor is invoked
whenever an object of its associated class is created. It is called constructor because it construct the value data members of the class. The constructor functions have some
special characteristics.  They should be declared in the public section.  They are invoked automatically when the objects are created.  They do not have return types,
not even void and therefore, they cannot return values.  They cannot be inherited, though a derived class can call the base class constructor. DEFAULT CONSTRUCTOR The
.

default constructor for any class is the constructor with no arguments. When no arguments are passed, the constructor will assign the values specifically assigned in the body
of the constructor. It can be zero or any other value. The default constructor can be identified by the name of the class followed by empty parentheses. Above program uses
default constructor. If it is not defined explicitly, then it is automatically defined implicitly by the system. PARAMETERIZED CONSTRUCTOR It is possible to pass arguments to
constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to
any other function. When you define the constructor's body, use the parameters to initialize the object. COPY CONSTRUCTOR: A Copy constructor takes a reference to an
object of the same class as itself as n argument . a constructor that initializes an objects using values of another object passed to it as parameter ,is called copy
constructor. DYNAMIC CONSTRUCTOT: allocation of memory to objects at the time of their construction is known asdynamic constructor. The memory is allocted with the
help of the new operator. The constructor can also be used to allocate memory while creating objects.

Describe use of protected access specifier used in the class. Protected access specifier is use to declare a class member that is accessible by the member functions within its
class and any class immediately derived from it. Object oriented programming language:  C++  Smalltalk  Object pascal  java  Simula  Ada  Turbo pascal  Eiffel  C# 
Python

.
With suitable example, describe effect of ++ and - - operators used with pointer in pointer arithmetic ++ Operator: - It is referred as increment operator that increments the
value of variable. If ++ operator is used with pointer variable, then pointer variable points to next memory address that means pointer increment with respect to size of the
data type used to declare pointer variable. - - Operator: - It is referred as decrement operator that decrements the value of variable. If - - operator is used with pointer
variable, then pointer variable points to previous memory address that means pointer decrement with respect to size of the data type used to declare pointer variable.

UNARY OPERATOR: are the operators which operate on one operand to perform operation . eg; i++,i--. The opertors cn be overloading using two different function i.e .
through member functions and friend functions. BINARY OPERTOR: it are opertors which operates on two operands to perform operartions . for examples , +, *,/,-,<,>,==.
Overloading binary opertors means , making the binary operators work on variables of user defined types.

Virtual Base Class: An ancestor class is declared as virtual base class which is used to avoid duplication of inherited members inside child class due to multiple path of
inheritance. Consider a hybrid inheritance as shown in the above diagram. The child class has two direct base classes, Parent1 and Parent2 which themselves have a common
base class as Grandparent. The child inherits the members of Grandparent via two separate paths. All the public and protected members of Grandparent are inherited into
Child twice, first via Parent1 and again via Parent2. This leads to duplicate sets of the inherited members of Grandparent inside Child class. The duplication of inherited
members can be avoided by making the common base class as virtual base class while declaring the direct or intermediate base classes as shown below.

Use of static data member: 1. Static data member is used to maintain values common to the entire class. 2. It is initialized to zero when the first object of its class is created.
3. Only one copy of that member is created for the entire class and is shared by all the objects of that class

Describe with examples, passing parameters to base class constructor and derived class constructor by creating object of derived class. When a class is declared, a
constructor can be declared inside the class to initialize data members. When a base class contains a constructor with one or more arguments then it is mandatory for the
derived class to have a constructor and pass arguments to the base class constructor. When both the derived and base classes contain constructors, the base constructor is
executed first and then the constructor in the derived class is executed. The constructor of derived class receives the entire list of values as its arguments and passes them on
to the base constructors in the order in which they are declared in the derived class.Describe how memory is allocated to objects of class with suitable diagram. Description:
The memory space for object is allocated when they are declared and not when the class is specified. Actually, the member functions are created and placed in memory
space only once when they are defined as a part of a class definition. Since all the objects belonging to that class use the same member functions, no separate space is
allocated for member functions. When the objects are created only space for member variable is allocated separately for each object. Separate memory locations for the
objects are essential because the member variables will hold different data values for different objects this is shown in fig

Describe following terms: Inheritance, data abstraction, data encapsulation, dynamic binding. Inheritance: 1. Inheritance is the process by which objects of one class acquire
the properties of objects of another class. 2. It supports the concept of hierarchical classification. It also provides the idea of reusability. Data abstraction: 1. Data abstraction
refers to the act of representing essential features without including the background details or explanations. 2. Classes use the concept of abstraction and are defined as a
list of abstract attributes such as size, weight and cost and functions to operate on these attributes. Data encapsulation: 1. The wrapping up of data and functions together
into a single unit (called class) is known as encapsulation. 2. By this attribute the data is not accessible to the outside world, and only those functions which are wrapped in
the class can access it. Dynamic Binding: 1. Dynamic binding refers to the linking of a procedure call to be executed in response to the call. 2. It is also known as late binding.
It means that the code associated with a given procedure call is not known until the time of the call at run-time.

State and describe visibility modes and its effects used in inheritance. Private members of base class are not inherited directly in any visibility mode. 1. Private visibility mode
In this mode, protected and public members of base class become private members of derived class. 2. Protected visibility mode In this mode, protected and public members
of base class become protected members of derived class. 3. Public visibility mode In this mode, protected members of base class become protected members of derived
class and public members of base class become public members of derived class.

Write any three rules of operator overloading. Rules for overloading operators: 1. Only existing operators can be overloaded. New operators cannot be created. 2. The
overloaded operator must have at least one operand that is of user defined data type. 3. We can’t change the basic meaning of an operator. That is to say, we can’t redefine
the plus(+) operator to subtract one value from other. 4. Overloaded operators follow the syntax rules of the original operators. They can’t be overridden. 5. There are some
operators that can’t be overloaded. 6. We can’t use friend functions to overload certain operators. However, member function scan be used to overload them

Need for Object oriented Programming Object-oriented programming scales very well, from the most trivial of problems to the most complex tasks. It provides a form of
abstraction that resonates with techniques people use to solve problems in their everyday life. Object-oriented programming was developed because limitations were
discovered in earlier approaches to programming. There were two related problems. First, functions have unrestricted access to global data. Second, unrelated functions and
data, the basis of the procedural paradigm, provide a poor model of the real world
Basic Concepts of Object oriented Programming 1. Class A class is a user defined data type. A class is a logical abstraction. It is a template that defines the form of an object.
A class specifies both code and data. It is not until an object of that class has been created that a physical representation of that class exists in memory. When you define a
class, you declare the data that it contains and the code that operates on that data. Data is contained in instance variables defined by the class known as data members, and
code is contained in functions known as member functions. The code and data that constitute a class are called members of the class. 2. Object An object is an identifiable
entity with specific characteristics and behavior. An object is said to be an instance of a class. Defining an object is similar to defining a variable of any data type. Space is set
aside for it in memory

Friend function In general, only other members of a class have access to the private members of the class. However, it is possible to allow a nonmember function access to
the private members of a class by declaring it as a friend of the class. To make a function a friend of a class, you include its prototype in the class declaration and precede it
with the friend keyword. The function is declared with friend keyword. But while defining friend function, it does not use either keyword friend or :: operator. A function can
be a friend of more than one class. Member function of one class can be friend functions of another class. In such cases they are defined using the scope resolution operator.
A friend, function has following characteristics.  It is not in the scope of the class to which it has been declared as friend.  A friend function cannot be called using the
object of that class. If can be invoked like a normal function without help of any object.  It cannot access the member variables directly & has to use an object name dot
membership operator with member name.  It can be declared either in the public or the private part of a class without affecting its meaning.  Usually, it has the object as
arguments.STATIC MEMBER FUNCTION: member function may also be declared as static. A static function can have access to only other static members declared in the
same class. But there are following restrictions on them:1) they may only access other statis members of the same class directly.2). They do not have a “this pointer”.3)there
cannot be a static and non static version of same function.STATIC DATA MEMBER : a static data members is useful when all objects of the same class must share a common
item of information. Static data members of a class are used to shared information among the objects of a class . static variables are normally used to maintain values
common of entire this value.CHARACTERISTICS: 1) all static vriables are automatically initialized to zero value , when the first object of the class is created .2)only one copy
of static data members is created for the entire class and is shraed by the all the objects of the class.3) it is visiable only within the class, but its lifetime is the entire
program.4) static data members are normally used to maintain values common for all the objects.

Pointer : a pointer is a special type of variable ,one that literally points to an address in memory of another variable. A standard variab,le holds a value, the value stored at
an address in memory , a pointer simply holds the address of another variable. this Pointer It is facilitated by another interesting concept of C++ called this pointer. ‘this’ is
a C++ keyword. ‘this’ always refers to an object that has called the member function currently. We can say that ‘this’ is a pointer. It points to the object that has called this
function this time. While overloading binary operators, we use two objects, one that called the operator function and the other, which is passed to the function. We referred
to the data member of the calling object, without any prefix. However, the data member of the other object had a prefix. Always ‘this’ refers to the calling object place of the
object name. pointer operators : 1. A pointer operator can be represented by a combination of * (asterisk) with a variable . the asterisk (*) tells the compiler that the
variable it follows is a pointer. 2. An addressed of operator can be represented by a combination of &(ampersand) with a pointer variable . it is used to get the addressed of
variable.

PolymorphismPolymorphism is a concept, which allows us to redefine the way something works, by either changing how it is done or by changing the parts using which it is
done. Both the ways have different terms for them.If we walk using our hands, and not legs, here we will change the parts used to perform something. Hence this is
called Overloading.And if there is a defined way of walking, but I wish to walk differently, but using my legs, like everyone else. Then I can walk like I want, this will be called
as Overriding.RUN TIME POLYMORPHISM: C++ supports run time polymorphism with the help of virtual function . it is called late or dynamic binding because the appropriate
function is selected dynamically at run time.

C++ Virtual Function A virtual function is a member function that is declared within a base class and redefined by a derived class. In order to make a function virtual, you
have to add keyword virtual in front of a function definition. When a class containing a virtual function is inherited, the derived class redefines the virtual function relative to
the derived class. The virtual function within the base class defines the form of the interface to that function. Each redefinition of the virtual function by a derived class
implements its operation as it relates specifically to the derived class. That is, the redefinition creates a specific method. When a virtual function is redefined by a derived
class, the keyword virtual is not needed. A virtual function can be called just like any member function. Pure virtual functions Sometimes when a virtual function is declared
in the base class, there is no meaningful operation for it to perform. This situation is common because often a base class does not define a complete class by itself. Instead, it
simply supplies a core set of member functions and variables to which the derived class supplies the remainder. When there is no meaningful action for a base class virtual
function to perform, the implication is that any derived class must override this function. To ensure that this will occur, C++ supports pure virtual functions. A pure virtual
function has no definition relative to the base class. Only the function prototype is included. To make a pure virtual function, use this general form: virtual type func-
name(parameter-list) = 0; The key part of this declaration is the setting of the function equal to 0. This tells the compiler that no body exists for this function relative to the
base class. When a virtual function is made pure, it forces any derived class to override it. If a derived class does not, a compile-time error results. Thus, making a virtual
function pure is a way to guaranty that a derived class will provide its own redefinition.

Abstract class When a class contains atleast one pure virtual function, it is referred to as an abstract class. Since, an abstract class contains atleast one function for which no
body exists, it is, technically, an incomplete type, and no objects of that class can be created. Thus, abstract classes exist only to be inherited. It is important to understand,
however, that you can still create a pointer to an abstract class, since it is through the use of base class pointers that run-time polymorphism is achieved. (It is also possible
to have a reference to an abstract class.)

Stream: a stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent. A
stream is genral name given a to flow of data. The stream that supplies data to the program is known as input stream and the one that receives data from the program is
known as output stream.

INHERITANCE: inheritance is an important feature of oops . inheritance can be described as the process of creating a new classes (called the derived class) from the existing
class(called the base class). SINGLE INHERITANCE: a derived class with only one base class is called single inheritance. MULTIPLE INHERITANCE : A derived class with more
than one base class is called multiple inheritance. HIERARCHICAL INHERITANCE: if one base class is inherited by more than one derived class , it is called hierarchical
inheritance. MULTILEVEL INHERITANCE: The mechanism of a deriving a class from another ‘ derived class ‘ is known as multilevel inheritance. HYBRID INHERITANCE: one
class is inherited by many derived classes which are again base class gor other derived classes. This form of in heritance is called hybrid iheritance . this form of inheritance
is called hybrid inheritance.

FUNCTION OVERLOADING: in c++ an user can create two or more functions that shares the same name, provided their parameters declarations are different .hence , the
functions that share the same name are said to be overloaded and the process is known as function overloading. RULES:1) the default arguments of the overloaded
functions are not considered as part of the parameter list by the c++ operator.2) the return type of overloaded functions may not be the same.3) each overloaded function
must differ either by the number of its normal parameters,thei data types or the sequence of the parameters. 4) using same name for two or more functions and
performing unrelated tasks is a bad practice..

You might also like