0% found this document useful (0 votes)
20 views17 pages

C++ Module 1 Notes

Module 1 introduces Object-Oriented Programming (OOP) principles such as encapsulation, abstraction, inheritance, and polymorphism, emphasizing their advantages in software development. It provides an overview of C++ as a programming language that supports OOP, along with basic syntax, class structures, and examples of key concepts. The module also covers various types of inheritance and the significance of abstract classes and polymorphism in enhancing code flexibility and reusability.

Uploaded by

shekhuzaif345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views17 pages

C++ Module 1 Notes

Module 1 introduces Object-Oriented Programming (OOP) principles such as encapsulation, abstraction, inheritance, and polymorphism, emphasizing their advantages in software development. It provides an overview of C++ as a programming language that supports OOP, along with basic syntax, class structures, and examples of key concepts. The module also covers various types of inheritance and the significance of abstract classes and polymorphism in enhancing code flexibility and reusability.

Uploaded by

shekhuzaif345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

MODULE 1

1.1 Introduction to Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of


objects, which can contain data (attributes) and code (methods). Unlike procedural
programming, which focuses on functions and logic, OOP emphasizes modelling real-world
entities and their interactions. This approach simplifies complex problems by breaking them
into smaller, manageable components.

Key principles of OOP:

 Encapsulation: Bundling data and methods within objects.

 Abstraction: Hiding unnecessary details and exposing only essential features.


 Inheritance: Reusing code by creating new classes from existing ones.

 Polymorphism: Allowing objects to take multiple forms based on context.

Advantages of OOP include modularity, code reusability, easier maintenance, and scalability.

1.2 Computer Programming Background

Before diving into OOP, it’s essential to understand basic programming concepts:

 Variables: Used to store data.

 Control structures: Conditional statements (if/else) and loops (for/while).


 Functions: Blocks of reusable code that perform specific tasks.
These foundational concepts are extended in OOP through classes and objects,
enabling a more organized approach to software development.

1.3 C++ Overview

C++ is a general-purpose programming language that supports both procedural and object-
oriented paradigms. It was developed by Bjarne Stroustrup as an extension of C. C++
introduced features like classes, inheritance, and polymorphism, making it ideal for large-
scale software development.

Key features:

 High performance due to close-to-hardware operations.

 Rich library support.

 Compatibility with C
1.4 First C++ Program

A basic C++ program follows this structure:

#include <iostream.h> // Header file for input/output operations

using namespace std; // Namespace declaration

void main() { // Entry point of the program

cout << "Hello World"; // Outputs "Hello World”

Output:

Hello, World

Explanation:
1. Header File Inclusion: Includes libraries like <iostream> for input/output operations.

2. Namespace: std avoids name conflicts in large programs.

3. Main Function: The execution starts here.

4. Output Statement: cout prints text to the console

1.5 Basic C++ Syntax

Understanding syntax is crucial for writing effective programs:

1. Data Types: int, float, char, string, etc., define the type of data stored in variables.
2. Operators: Arithmetic (+, -), relational (>, <), logical (&&, ||).

3. Control Statements: Conditional (if/else) and iterative (for, while) structures.

4. Functions: Declared using a return type followed by the function name4.

1.6 Classes

A class is a blueprint for creating objects. It defines the structure (attributes) and behavior
(methods) common to all objects of that type.

Example:

class Car {

public:

string brand;
void start() {

cout << "Car started";

Here, Car is a class with a public attribute brand and method start().

1.7 What is an Object?

An object is an instance of a class that encapsulates data and methods. Objects represent real-
world entities with attributes (data) and behaviors (methods).

Example:

A car object might have:

 Attributes: Color, model, brand.

 Methods: Start(), stop(), accelerate()5.

1.7 Methods and Messages


Methods are functions defined within a class that operate on its attributes. Messages refer to
calls made to these methods by objects.
Example:

Car myCar;

myCar.start(); // Message sent to invoke the 'start' method

Output:

Car Started

1.8 Abstraction
Abstraction hides implementation details while exposing only essential functionalities. For
example, when driving a car, you interact with controls like the steering wheel or accelerator
but do not need to understand how the engine works internally.

In programming:

 Abstract classes or interfaces define methods without implementing them.


 This promotes modularity and reduces complexity
Ways to Achieve Abstraction in C++

There are two primary ways to implement abstraction in C++:

1. Using Classes:

 Classes group data members and member functions.

 Access specifiers (private, protected, public) control which parts of the class
are visible to the outside world.

 Private members are hidden from external access, ensuring data security and
encapsulation.

Example:

#include <iostream.h>

using namespace std;

class AbstractionExample {

private:

int data1, data2; // Private members are hidden

public:

// Public method to set values

void setValues(int a, int b) {

data1 = a;

data2 = b;

// Public method to display values


void displayValues() {

cout << "Data1: " << data1 << ", Data2: " << data2 << endl;

}
void main() {

AbstractionExample obj;

obj.setValues(10, 20); // Setting values using public method

obj.displayValues(); // Displaying values using public method

Output:

Data1: 10, Data2:20

2. Using Abstract Classes:

 Abstract classes contain at least one pure virtual function (virtual void
functionName() = 0;).

 These classes act as blueprints for derived classes, enforcing specific behavior
while hiding implementation details.

Example:
#include <iostream.h>

#include <string.h>

using namespace std;

// Abstract class

class Shape {

protected:

string name;

public:

Shape(const string& shapeName) : name(shapeName) {}

// Pure virtual function

virtual void draw() const = 0;


virtual ~Shape() {} // Virtual destructor for cleanup

// Derived class

class Circle : public Shape {

private:

float radius;

public:

Circle(const string& shapeName, float r) : Shape(shapeName), radius(r) {}

void draw() const override {

cout << "Drawing " << name << " with radius " << radius << endl;

void main() {

Circle circle("Circle", 5.0f);


circle.draw(); // Calls overridden draw() method

Output:

Drawing Circle with radius 5


1.9 Encapsulation

Encapsulation bundles data and methods within an object while restricting access to internal
details using access modifiers (private, protected, public). This ensures data security.

Example:

class BankAccount {

private:

int balance;
public:

void deposit(int amount) {

balance += amount;

Here, balance is private and can only be modified through public methods like deposit()

1.10 Inheritance
Inheritance allows one class (child) to acquire properties and behaviors from another class
(parent). This promotes code reuse.

Types of Inheritance:

1. Single Inheritance

2. Multiple Inheritance

3. Multilevel Inheritance

4. Hierarchical Inheritance

Single Inheritance: Single inheritance is one of the simplest inheritance among other types
of inheritance in C++, in which the child class is derived only from the single parent class.
Syntax:

class A

class B: visibility_mode A

}
Example:

#include <iostream.h>

using namespace std;

// base class

class Animal

public:
// constructor of the base class

Animal()

cout << "I am an animal.\n";

// derived class
class Dog: public Animal

public:
// constructor of the derived class

Dog()

cout << "I am a dog.\n";


}

void main()

// create an object of the child's class

Dog obj;

return 0;

}
Output:

I am an animal.

I am a dog.

Multiple Inheritance: Multiple inheritances are another type of inheritance in c++ in which
the child class inherits its properties from more than one base class. This means the child
class is inherited from multiple parent classes, so the child class can derive the combined
features of all these classes. The child class accesses all base class data members according to
the visibility mode used.
Syntax:

class A

class B

}
class C: visibility_mode_1 A, visibility_mode_2 B

Example:

#include <iostream.h>
using namespace std;

// base class

class Animal

public:

// constructor of the base class

Animal()
{

cout << "I am an animal.\n";

}
}

class Speak

public:

// constructor of the base class

Speak()

cout << "I can speak.\n";

// derived class

class Dog: public Animal, public Speak


{

void main()

// create an object of the child's class

Dog obj;

}
Output:

I am an animal.

I can speak.

Multilevel Inheritance: Multilevel inheritance is one type of inheritance in C++ in which a


derived class inherits all its properties from a class that itself inherits from another class, i.e.,
the parent class for one is a child class for the other.
Syntax:

class A

class B: visibility_mode A

class C: visibility_mode B

Example:

#include <iostream.h>

using namespace std;

class Animal
{

public:

// constructor of the base class


Animal()

cout << "I am an animal.\n";

class Dog: public Animal

public:

// constructor of the derived class

Dog()

cout << "I am a dog.\n";


}

class Pug: public Dog

public:

Pug()

{
cout << "I am a pug.\n";

void main()

// create an object of the child's class

Pug obj;
}

Output:

I am an animal.
I am a dog.

I am a pug.

Hierarchical Inheritance: In hierarchical inheritance, multiple child classes can inherit the
property from a single-parent class.

Syntax:

class A
{

};

class B: visibility_mode A

class C: visibility_mode A

class D: visibility_mode A
{

Example:

// base class

class Animal

public:

// constructor of the base class

Animal()
{

cout << "I am an animal.\n\n";

// derived class

class Dog: public Animal

{
}

// derived class

class Pig: public Animal

};

// derived class

class Cat: public Animal


{

};

void main()
{

// create object of the child class

Dog obj1;

Pig pbj2;

Cat obj3;

Output:

I am an animal.

I am an animal.

I am an animal.

1.11 Abstract Classes


An abstract class cannot be instantiated directly but serves as a base for other classes. It
contains pure virtual functions that must be implemented by derived classes.
Example:

class Shape {

public:

virtual void draw() = 0; // Pure virtual function

class Circle : public Shape {


public:

void draw() {

cout << "Drawing Circle";

}
1.12 Polymorphism

Polymorphism allows objects to behave differently based on context. It can be achieved


through method overriding or overloading.

Example:

class Animal {

public:

virtual void sound() {

cout << "Animal sound";

class Dog : public Animal {

public:

void sound() override {


cout << "Bark";

Animal* pet = new Dog();

pet->sound(); // Outputs "Bark" due to polymorphism

Output:
Bark

Polymorphism ensures flexibility in code design

This detailed explanation covers all topics mentioned in Module-1 comprehensively with
examples where applicable.

You might also like