Unit 1 Version1
Unit 1 Version1
Unit – 1
CO3 Specify simple abstract data types and design implementations using abstraction PO[1,2,4,6,12]
functions to document
them.
CO4 Design a convenient way for the handling problems using templates and use PO[1,2,3,4,5,6,7,12]
simple try-catch blocks for
Exception Handling.
CO5 Manage I/O streams and File I/O oriented interactions. PO[1,2,3,4,5,6,9,11,12]
Assessment and Evaluation Plan
Languages used in Procedural Programming
C, FORTRAN, ALGOL, COBOL,
Languages used in Object-Oriented Programming
Java, C++, C#, Python,
PHP, JavaScript, Ruby, Perl,
Prepared by : Dr. Ahmad Jamal
Procedural Oriented Programming Object-Oriented Programming
In procedural programming, the program is divided into In object-oriented programming, the program is divided
small parts called functions. Heading into small parts called objects.
There is no access specifier in procedural programming. Object-oriented programming has access specifiers like
private, public, protected, etc.
Adding new data and functions is not easy. Adding new data and function is easy.
Procedural programming does not have any proper way of Object-oriented programming provides data hiding so it is
hiding data so it is less secure. more secure.
In procedural programming, there is no concept of data In object-oriented programming, the concept of data
hiding and inheritance. hiding and inheritance is used.
Procedural programming is based on the unreal world. Object-oriented programming is based on the real world.
Procedural programming is used for designing medium- Object-oriented programming is used for designing large
sized programs. and complex programs.
Code reusability absent in procedural programming, Code reusability present in object-oriented programming.
Examples: C, FORTRAN, Pascal, Basic, etc. Examples: C++, Java, Python, C#, etc
Advantages of Encapsulation:
Data Hiding:
Increased Flexibility:
Reusability:
Testing code is easy:
Prepared by : Dr. Ahmad Jamal
Inheritance
The capability of a class to derive properties and characteristics from another class is called
Inheritance. Inheritance is one of the most important features of Object-Oriented Programming.
Inheritance is a feature or a process in which, new classes are created from the existing classes.
The new class created is called “derived class” or “child class” and the existing class is known as
the “base class” or “parent class”. The derived class now is said to be inherited from the base
class.
When we say derived class inherits the base class, it means, the derived class inherits all the
properties of the base class, without changing the properties of base class and may add new
features to its own. These new features in the derived class will not affect the base class. The
derived class is the specialized class for the base class.
Sub Class: The class that inherits properties from another class is called Subclass or Derived
Class.
Super Class: The class whose properties are inherited by a subclass Prepared
is calledbyBase ClassJamal
: Dr. Ahmad or
Inheritance
Method calls, also known as function calls, are used to invoke a particular member function
of an object. You call a member function on an instance of the class using the dot operator
(.).
Example:
int main() {
MyClass obj;
obj.printMessage(); // Calling the member function
return 0;
}
C++ was created by Bjarne Stroustrup in the early 1980s. It was developed to extend the capabilities of the C
programming language by adding support for object-oriented programming. The name "C++" reflects this
extension; it signifies "incremented C."
C++ syntax shares many similarities with C, but with added features for OOP. Here's a simple example of a
"Hello, World!" program in C++:
5: IDEs and Tools: Various Integrated Development Environments (IDEs) and text editors support C++
development. Examples include Dev C++, Visual Studio, Visual Studio Code etc.
C++ provides a balance between high-level abstractions and low-level control, making it suitable for a diverse
set of programming tasks. It has a rich ecosystem of libraries and tools that contribute to its popularity and
versatility.
Objects:
• Instances of classes, representing specific data and behavior.
• Created based on class templates.
• Access class attributes and methods through objects.
Usage:
Structures are often used for grouping related variables together.
They do not support member functions (methods) by default, although you can define functions outside the
structure to operate on its data.
struct Point {
double x;
double y;
};
int main() {
Point p;
p.x = 3.0;
p.y = 5.0;
return 0;
}
Usage:
Classes are used for creating objects that encapsulate data and behavior.
They can have member functions (methods) which allow the manipulation of data.
public:
double calculateArea() {
return 3.14159 * radius * radius;
}
};
int main() {
Circle c;
c.radius = 4.0; // This would be an error since radius is private
c.calculateArea();
return 0;
}
• Both structures and classes can be used to group data members together.
• Both can have constructors and destructors for initialization and cleanup.
• Both can use access specifiers (public, private, protected) to control
member visibility.
• Use structures when you need a simple data container with no specific
behavior.
• Use classes when you need to encapsulate data and behavior, and when
you want to follow the principles of object-oriented programming.
Prepared by : Dr. Ahmad Jamal
Unions and Classes
Union:
Memory Sharing:
All members of a union share the same memory location. This means that only one member can
hold a value at a time.
Changing the value of one member affects the other members.
Memory Size:
The memory size of a union is determined by the largest member.
This can be useful for conserving memory when you need to store different types of data at
different times.
Accessing Members:
You can access members of a union using the dot (.) operator, just like with structures.
Only one member should be accessed at a time, and it's your responsibility to know which member
is currently valid.
Use Cases:
Used when you want to save memory by storing different types of data in the same memory
location.
Useful for scenarios where only one member needs to hold a value at a time, like different types of
statuses.
Prepared by : Dr. Ahmad Jamal
Classes:
Memory Allocation:
Each member of a class has its own separate memory space.
Changing the value of one member does not affect the others.
Memory Size:
The memory size of a class is the sum of the memory used by its members and any padding
added for alignment.
Member Functions:
Classes can have member functions (methods) that provide behavior and operations related to the
data members.
Encapsulation allows you to control access to the members and enforce proper usage.
Object Creation:
Objects of a class can be created, which encapsulate data and behavior.
Each object has its own separate memory for data members.
Use Cases:
Used when you want to group related data and behavior together in a modular and organized way.
Suitable for creating instances of objects that can interact with each other.
Protecte
Public Private
d
Access modifiers in C++ are keywords that determine the visibility and
accessibility of class members (attributes and methods) from different parts
of the program. C++ supports three main access modifiers: public, private,
and protected. These modifiers control the level of encapsulation and the
ability to interact with class members from various contexts.
Example:
class MyClass {
public:
int publicAttribute;
void publicMethod() {
// ...
}
};
Example:
class MyClass {
private:
int privateAttribute;
void privateMethod() {
// ...
}
};
class BaseClass {
protected:
int protectedAttribute;
void protectedMethod() {
// ...
}
};
class MyClass {
public: // Accessible everywhere
int publicAttribute;
Friend functions are often used in situations where you want to provide external functions with special
privileges to access class members that are not accessible to the general public.
Friend Declaration:
A class can be declared as a friend of another class using the friend keyword in the class that is
granting access.
This allows the friend class to access private and protected members of the class it's friends with.
Access Control:
A friend class has access to all the members (including private members) of the class it's friends
with.
The friend class does not inherit from the class it's friends with; it simply gains access to its
members.
#include <iostream>
// Inline function declaration
inline int add(int a, int b) {
return a + b;
}
int main() {
int x = 5, y = 10;
// Inline function call
int result = add(x, y);
std::cout << "Result: " << result << std::endl;
return 0;
}
Inline functions are typically used for small, frequently called functions where the overhead of the
function call is more significant than the actual function execution. However, using inline excessively or
on large functions can actually lead to larger executable sizes and might not always result in
performance gains.
Modern C++ compilers often perform automatic inlining as part of their optimization process without
explicitly using the inline keyword. The inline keyword provides a suggestion to the compiler, but the
compiler can make its own decisions based on the code and optimization settings.
Static members are defined using the static keyword within the class
definition.
Scope Resolution Operator is crucial for resolving ambiguity and accessing names defined in different
scopes. It's a key tool for managing namespaces, classes, and their members in C++.