CPP Unit 1
CPP Unit 1
The program is divided into small The program is divided into small
parts called functions. parts called objects.
Does not have access specifier. Has access specifiers such as private,
public, protected, etc.
Predefined functions
• A predefined function is a function available in a procedural
programming language from a library of available functions.
• Allow a programmer to complete common tasks without creating the
required code themselves.
2|Page 186_Shraddha Keshao Bonde_N2
CM3104
Local Variable
• A local variable is a programming variable that has a local scope of use.
• This means the variable only functions in the function in which the
developer defines it.
Global Variable
• Global variables increase functionality when local variables are
insufficient.
• Developers can use global variables in nearly all functions.
• A variable makes itself available to all methods and functions in the code,
allowing the developer to access key data throughout the program's many
procedures.
Modularity
• Modularity is a structure in which a developer divides the functionality of
its code into a series of smaller blocks.
• The programmer can then call these blocks, often called methods or
functions in procedural programming languages, in their code to access
them.
Parameter Passing
• Parameters are the data values that transfer from each function within a
code sequence.
• Parameter passing allows variable values to be passed through to the
program which will hadle it with a procedure.
Object
• It is a basic unit of Object-Oriented Programming and represents the real-
life entities.
Encapsulation:
• Encapsulation is defined as the wrapping up of data under a single unit.
• It is the mechanism that binds together code and the data it manipulates.
• The variables or data of a class are hidden from any other class and can
be accessed only through any member function of their class in which
they are declared.
• The data in a class is hidden from other classes, so it is also known as
data-hiding.
Inheritance
• Inheritance is an important pillar of OOP(Object-Oriented Programming).
• The capability of a class to derive properties and characteristics from
another class is called Inheritance.
• Inheritance allows the user to reuse the code whenever possible and
reduce its redundancy.
Polymorphism
• 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.
• For example, A person at the same time can have different characteristics.
Like a man at the same time is a father, a husband, an employee.
• So the same person posses different behavior in different situations. This
is called polymorphism.
Abstraction
• Data abstraction is one of the most essential and important features of
object-oriented programming.
• Data abstraction refers to providing only essential information about the
data to the outside world, hiding the background details or
implementation.
Class
• A class is a user-defined data type.
• It consists of data members and member functions, which can be accessed
and used by creating an instance of that class.
• It represents the set of properties or methods that are common to all
objects of one type. A class is like a blueprint for an object.
• Code Modularity
o Everything in OOP is an object
o These objects can be interchanged or removed to meet the users’ needs.
• Easier maintenance
o Inheritance usually reduces maintenance because of the ‘domino effect it has on
derived classes when a change is made in a base class.
• Design stability
o Once a stable base class has been developed, the new classes that are derived
may have fewer less errors and bugs.
What is C++?
➢ C++ is a cross-platform language that can be used to create high-
performance applications.
➢ C++ was developed by Bjarne Stroustrup, as an extension to the C
language.
➢ C++ gives programmers a high level of control over system resources and
memory.
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
➢ The name cout is short for character output and displays whatever is
between the << brackets.
➢ Symbols such as << can also behave like functions and are used with the
keyword cout.
➢ The return keyword tells the program to return a value to the function int
main
➢ After the return statement, execution control returns to the operating
system component that launched this program.
➢ Execution of the code terminates here.
C++ Classes/Objects
✓ Everything in C++ is associated with classes and objects, along with its
attributes and methods.
✓ For example: in real life, a car is an object. The car has attributes, such as
weight and color, and methods, such as drive and brake.
✓ Attributes and methods are basically variables and functions that belongs
to the class. These are often referred to as "class members".
✓ A class is a user-defined data type that we can use in our program, and it
works as an object constructor, or a "blueprint" for creating objects.
Create a Class
To create a class, use the class keyword:
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
10 | P a g e 186_Shraddha Keshao Bonde_N2
CM3104
Create an Object
To create an object of MyClass, specify the class name, followed by the
object name.
class MyClass
{ // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 15;
myObj.myString = "Some text";
Linking
✓ Refers to creation of a single executable file from multiple object files.
✓ The file created after linking is ready to be loaded into memory and
executed by the system.
✓ There is difference in linking and compilation when it comes to
understanding errors.
Compiling
✓ This phase translates the program into a low level assembly level code.
✓ The compiler takes the preprocessed file ( without any directives) and
generates an object file containing assembly level code.
✓ The object file created is in the binary form.
****************************************************************