Nothing Special   »   [go: up one dir, main page]

CH 1 OOP - Introduction

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Chapter 1 – Introduction to OOP in C++ Author: Irfan

Introduction to Object-Oriented
Programming (OOP) in C++
1.1 Overview of Object-Oriented Programming
Object-Oriented Programming (OOP) is a programming paradigm that uses 'objects' to
design applications and computer programs. It incorporates several principles and concepts
such as:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction

1.2 Key Concepts of OOP

Objects:
Objects are instances of classes. They contain data (attributes) and methods (functions) that
manipulate the data.

Classes:
A class is a blueprint for creating objects. It defines a datatype by bundling data and
methods.

Encapsulation:
Encapsulation is the mechanism of restricting access to some of the object's components. It
means that the internal representation of an object is hidden from the outside. Achieved
using access specifiers: private, protected, and public.

Inheritance:
Inheritance is a way to form new classes using classes that have already been defined. The
new class (derived class) inherits attributes and methods from the existing class (base
class). Promotes code reuse.

Polymorphism:
Polymorphism allows methods to do different things based on the object it is acting upon.
Achieved through method overloading and method overriding.

Abstraction:
Abstraction is the concept of hiding the complex implementation details and showing only
the necessary features. Achieved using abstract classes and interfaces.
Chapter 1 – Introduction to OOP in C++ Author: Irfan

1.3 Advantages of OOP


- Modularity: The source code for an object can be written and maintained independently
of the source code for other objects.
- Reusability: Objects can be reused across programs.
- Pluggable and Debuggable: If a particular object turns out to be problematic, it can be
removed and replaced with a better object.

1.4 Comparison with Procedural Programming

- Procedural Programming: - Object-Oriented Programming:


• Focuses on functions or procedures • Focuses on objects which contain
to operate on data. both data and functions.
• Data and functions are separate. • Promotes better organization and
management of code.

1.5 C++ as an OOP Language


C++ is a multi-paradigm programming language that supports both procedural and object-
oriented programming. Key features supporting OOP in C++ include:
- Classes and Objects
- Constructors and Destructors
- Inheritance
- Polymorphism
- Templates
- Exception Handling

1.6 Basic Structure of a C++ Program


Here is a simple C++ program demonstrating the creation of a class and object:

#include <iostream>
using namespace std;

// Class Definition
class Car {
public:
string brand;
string model;
int year;

void printDetails() {
cout << "Brand: " << brand << "\nModel: " << model <<
Chapter 1 – Introduction to OOP in C++ Author: Irfan

"\nYear: " << year << endl;


}
};

// Main Function
int main() {
// Creating an object of Car
Car car1;
car1.brand = "Toyota";
car1.model = "Corolla";
car1.year = 2020;

// Accessing method
car1.printDetails();

return 0;
}

1.7 Access Specifiers in C++


- Private: Members are accessible only within the same class.
- Protected: Members are accessible within the same class and derived classes.
- Public: Members are accessible from any part of the program.

Example:

class Box {
private:
double length;
public:
double width;
protected:
double height;
};

1.8 Constructors and Destructors


- Constructor:
- Special function called when an object is instantiated.
- Used to initialize objects.
- Syntax: ClassName(parameters) { // body }

- Destructor:
- Special function called when an object is destroyed.
Chapter 1 – Introduction to OOP in C++ Author: Irfan

- Used to release resources.


- Syntax: ~ClassName() { // body }

Example:

class Box {
public:
double length;
Box() {
length = 0.0;
}
~Box() {
// Destructor code
}
};

1.9 Inheritance in C++


Enables new classes to inherit properties and behaviors from existing classes.
Syntax: class DerivedClass : accessSpecifier BaseClass { // body }

Example:

class Shape {
public:
void setWidth(int w) {
width = w;
}
protected:
int width;
};

class Rectangle: public Shape {


public:
int getArea() {
return (width * height);
}
void setHeight(int h) {
height = h;
}
private:
int height;
};
Chapter 1 – Introduction to OOP in C++ Author: Irfan

1.10 Polymorphism in C++


- Compile-time Polymorphism:
- Function Overloading
- Operator Overloading

- Run-time Polymorphism:
- Virtual Functions

Example:

class Base {
public:
virtual void show() {
cout << "Base class" << endl;
}
};

class Derived : public Base {


public:
void show() {
cout << "Derived class" << endl;
}
};

int main() {
Base* b;
Derived d;
b = &d;
b->show(); // Outputs: Derived class
return 0;
}

You might also like