Professional Documents
Culture Documents
C++ Basics
Unit: IV
Object Oriented Programming
Ritu Agarwal
1
B Tech 6th Semester
Associate Professor
IT Department
Faculty Information
Faculty Information
2
• E-Mail: ritu7fit@rkgit.edu.in
4
At the end of the semester, the student will be able to:
Understand the functionality and basic concepts of object oriented design
techniques.
Understand and apply various object oriented features like inheritance, data
abstraction, encapsulation and polymorphism to solve various computing
problems.
Appreciate the dynamic memory allocation functions, access specifiers and the
friend functions.
Understanding the basics of C and C++.
Implementing the subprogram call and return.
Course Outcome
5
Understand the application development and analyze the insights of object
oriented programming to implement application
Understand, analyze and apply the role of overall modeling concepts (i.e.
System, structural).
Understand, analyze and apply oops concepts (i.e. abstraction, inheritance).
Understand the basic concepts of C++ to implement the object oriented
concepts.
To understand the object oriented approach to implement real world problem.
Program Outcome
6
1. Engineering knowledge: Apply the knowledge of mathematics, science,
engineering fundamentals, and an engineering specialization for the solution of
complex engineering problems.
2. Problem analysis: Identify, formulate, research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering
problems and design system components or processes that meet the specified
needs with appropriate consideration for public health and safety, and cultural,
societal, and environmental considerations.
4. Conduct investigations of complex problems: Use research based knowledge
and research methods including design of experiments, analysis and interpretation
of data, and synthesis of information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources,
and modern engineering and IT tools, including prediction and modeling to
complex engineering activities, with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
7 to the professional engineering practice.
relevant
7. Environment and sustainability: Understand the impact of the professional
engineering solutions in societal and environmental contexts, and demonstrate the
knowledge of, and need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with the society at large, such as, being able to comprehend
and write effective reports and design documentation, make effective presentations, and
give and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
CO-PO Mapping
CO PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2
2 2 2 1 2 2 - - - - - 2 2 2
CO1
2 2 3 1 2 2 - - - - - 2 2 2
CO2
2 2 2 1 2 1 1 - - - - 2 2 2
CO3
2 2 2 1 2 - 1 - - - - 2 2 2
CO4
2 2 3 1 2 - - - - - - 2 2 2
CO5
2 2 2.4 1 2 1.6 1 - - - - 2 2 2
CO
End Semester Question Paper Template
9
Brief Introduction about the Subject with Videos
Videos: https://www.youtube.com/watch?v=LpU2h-4iEys
https://www.youtube.com/watch?v=hozM7JhFFXg
10
Prerequisites and Revision
11
In the Unit-1 students learnt the basic functionality and basic concepts of object
oriented design techniques and UML modelling.
In Unit-2, the students will understand the Structural model: a view of an system,
that emphasizes the structure of the objects, including their classifiers, relationships,
attributes and operations and the structural relationships.
In Unit-3, the students will understand dynamic memory allocation functions, access
specifiers and the friend functions, Understanding the basics of C and C++,
Implementing the subprogram call and return.
Overview of C language:
1. C++ can be considered as an incremental version of c language which consists all programming language constructs with newly added features of
object oriented programming.
2. C++ is object oriented programming language.
3. The file extension of C++ program is “.CPP”
4. Function overloading and operator overloading are possible.
5. Variables can be declared in inline i.e. when required
6. In c++ more emphasis is give on data rather than procedures.
7. Polymorphism, encapsulation and inheritance are possible.
8. Data abstraction property is supported by c++.
9. Data access is limited. It can be accessed by providing various visibility modes both for data and member functions.
10.Dynamic binding is supported by C++
11.It supports all features of c language
12.It can be called as an incremental version of c language
Difference Between Procedure Oriented Programming
(POP) & Object Oriented Programming (OOP)
Difference Between Procedure Oriented Programming
(POP) & Object Oriented Programming (OOP)
BASIC STRUCTURE OF C++ LANGUAGE:
A C++ program should have one or more sections but the sequence of sections is to be
followed.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section & class declarations
5. Member function definition
6. Main function
1. DOCUMENTATION
SECTION :
1. Document section comes first and is used to document the use of logic or reasons in
your program.
2. It can be used to write the program's objective, developer and logic details.
3. The documentation is done in C++ language with /* and */ . Whatever is written
between these two are called comments.
2. LINKING SECTION :
1. This section tells the compiler to link the certain occurrences of keywords or functions in your
program to the header files specified in this section.
2. e.g. #include<iostream>
using namespace std;
3. Directive causes the preprocessor to add the contents of the iostream file to the program. It
contains declarations for cout and cin.
4. cout is a predefined object that represents the standard output stream. The operator << is an
insertion operator, causes the string in double quotes to be displayed on the screen.
5. The identifier cin is a predefined object in C++ that corresponds to the standard input stream. The
operator >> is known as extraction operator. It extracts the value from the keyboard and assigns it
to the value variable on its right.
3. DEFINITION SECTION :
1. It is used to declare some constants and assign them some value. e.g. #define MAX 25
2. Here #define is a compiler directive which tells the compiler whenever MAX is found
in the program replace it with 25.
4. GLOBAL DECLARATION
SECTION :
1. Here the variables and class definitions which are used through out the program (including
main and other functions) are declared so as to make them global(i.e
accessible to all parts of program).
2. A CLASS is a collection of data and functions that act or manipulate
the data. The data components of a class are called data members and function
components of a class are called member functions.
3. A class can also termed as a blue print or prototype that defines the variable or functions
common to all objects of certain kind.
4. It is a user defined data type.
5. e.g. int i; //this declaration is done outside and before main()
5. SUB PROGRAM OR
FUNCTION SECTION :
1. ex:
#include<iostream>
using namespace std;
namespace sample
{
int m;
void display(int n)
{
cout<<"in namespace N="<<n<<endl;
}
}
IDENTIFIERS:
1. Identifiers are the names given to various program elements such as variables, functions and arrays.
2. These are user defined names consisting of sequence of letters and digits.
3. Rules for declaring identifiers:
The first character must be an alphabet or underscore.
It must consist of only letters, digits and underscore.
Identifiers may have any length but only first 31 characters are significant.
It must not contain white space or blank space.
We should not use keywords as identifiers.
Upper and lower case letters are different.
IDENTIFIERS:
1. The declaration of variable gives the name for memory location and its size.
2. It specifies the range of value that can be stored in that location.
3. Syntax:
Data_type variable_name;
4. Ex:
int a=10;
float x=2.3;
Constants:
1. Constants refer to fixed values that the program may not alter and they are called literals.
2. Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-
Point Numerals, Characters, Strings and Boolean Values.
3. Again, constants are treated just like regular variables except that their values cannot be modified
after their definition.
4. Integer Literals: An integer literal can be a decimal, octal, or hexadecimal constant. A prefix
specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
85 // decimal
0213 // octal
0x4b // hexadecimal
Enum:
sizeof
1 sizeof operator returns the size of a variable. For example, sizeof(a), where ‘a’ is integer, and will return 4.
Condition ? X : Y
2 Conditional operator (?). If Condition is true then it returns value of X otherwise returns value of Y.
3 Comma operator causes a sequence of operations to be performed. The value of the entire comma expression is the value of the last expression of the comma-separated list.
4 Member operators are used to reference individual members of classes, structures, and unions.
Cast
5 Casting operators convert one data type to another. For example, int(2.2000) would return 2.
&
6 Pointer operator & returns the address of a variable. For example &a; will give actual address of the variable.
7 Pointer operator * is pointer to a variable. For example *var; will pointer to a variable var.
Precedence of Operators :
typecasting:
8. x = x + y; z = 108
1. Conversion using Cast operator: A Cast operator is an unary operator which forces one data type to be converted into another data
type.
2. int main()
3. { Click to add text
4. float f = 3.5;
5. // using cast operator
6. int b = static_cast<int>(f);
7. cout << b;
8. }
9. Output:
10. 3
Control Structures:
Control
Statement
while do for
Control Structures:
{ {
Statement-2;
True-Block Statement(s);
}
}
}
Else
else
{ {
False-Block statement(s); Statement-3;
} }
Control Structures:
Control Structures:
Switch(expression)
{
Case value-1:
block-1;
break;
Case value-2:
block-2;
break;
……
……
default:
default-block;
break;
}
Control Structures:
It is also called preprocessors directive. The macros are defined by the #define keyword. Before the
program compilation, the preprocessor examines the program whenever the preprocessor detects the macros
then preprocessor replaces the macro by the macro definition.
Syntax of Macro:
#define MACRO_NAME Macro_definition
Example of Macro:
#include <iostream>
using namespace std;
// macro with parameter
#define MAXIMUM(a, b) (a > b) ? a : b
// Main function for the program Output:
int main()
{
Max (100, 1000):1000 Max (20, 0):20
cout << "Max (100, 1000):";
int k = MAXIMUM(100, 1000);
cout << k << endl;
cout << "Max (20, 0):";
int k1 = MAXIMUM(20, 0);
cout << k1;
return 0;
}
Difference between inline FUNCTION AND MACRO
1. An inline function is defined by the inline keyword. Whereas the macros are defined by the #define keyword.
2. Through inline function, the class’s data members can be accessed. Whereas macro can’t access the class’s data members.
In C++, inline may be defined either inside the class or outside the Whereas the macro is all the time defined at the beginning of the
5.
class. program.
In C++, inside the class, the short length functions are automatically
6. While the macro is specifically defined.
made the inline functions.
7. Inline is not as widely used as macros. While the macro is widely used.
8. Inline is not used in competitive programming. While the macro is very much used in competitive programming.
A friend function of a class is defined outside that class' scope but it has the right to access
all private and protected members of the class. Even though the prototypes for friend
functions appear in the class definition, friends are not member functions.
A friend can be a function, function template, or member function, or a class or class
template, in which case the entire class and all of its members are friends.
To declare a function as a friend of a class, precede the function prototype in the class
definition with keyword friend.
Characteristics of a Friend function:
• The function is not in the scope of the class to which it has been declared as a friend.
• It cannot be called using the object as it is not in the scope of that class.
• It can be invoked like a normal function without using the object.
• It cannot access the member names directly and has to use an object name and dot
membership operator with the member name.
Friend function
A virtual function is a member function which is declared within a base class and is re-
defined(Overridden) by a 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.
• Virtual functions ensure that the correct function is called for an object, regardless of the
type of reference (or pointer) used for function call.
• They are mainly used to achieve Runtime polymorphism
• Functions are declared with a virtual keyword in base class.
• The resolving of function call is done at Run-time
virtual
#include <iostream>
function
using namespace std;
class base {
public:
void fun_1() { cout << "base-1\n"; } int main()
virtual void fun_2() { cout << "base-2\ {
base* p;
n"; } derived obj1;
virtual void fun_3() { cout << "base-3\ p = &obj1;
n"; } // Early binding because fun1() is non-virtual in base
p->fun_1();
virtual void fun_4() { cout << "base-4\ // Late binding (RTP)
n"; } p->fun_2();
}; // Late binding (RTP)
p->fun_3();
// Late binding (RTP)
class derived : public base { p->fun_4();
public: p->fun_4(5);
}
void fun_1() { cout << "derived-1\n"; }
void fun_2() { cout << "derived-2\n"; }
void fun_4(int x) { cout << "derived-4\
n"; }
};
virtual function
Thank You.