0% found this document useful (0 votes)
28 views

Mod 2 C++

Uploaded by

18kailashs2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Mod 2 C++

Uploaded by

18kailashs2020
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Certainly!

Here’s an expanded version of the notes, offering a more detailed explanation of


each concept from "Programming with ANSI C++" by Bhushan Trivedi.

Introduction to Object-Oriented Programming


Computer Programming Background

● Definition: Programming is the process of designing and writing code that instructs a
computer to perform specific tasks. This involves creating algorithms and translating
them into a language that the computer can understand and execute.
● Evolution:
○ Procedural Programming: Early programming focused on procedures or
routines. Programs were structured as a sequence of instructions that
manipulate data.
○ Object-Oriented Programming (OOP): Introduced to address limitations of
procedural programming by organizing code into objects that represent
real-world entities and concepts. This approach improves code reusability,
scalability, and maintenance.

C++ Overview

● Origin: Developed by Bjarne Stroustrup in the early 1980s, C++ was designed to
provide higher-level abstractions while retaining the efficiency and performance of C.
● Features:
○ Syntax: C++ retains the basic syntax of C, including the use of functions,
loops, and conditionals. However, it adds new constructs such as classes and
objects.
○ OOP Capabilities: C++ introduces several key OOP concepts including
classes, inheritance, polymorphism, and encapsulation.
○ Standard Libraries: Includes libraries like the Standard Template Library
(STL) which provides common data structures and algorithms.

First C++ Program


Basic C++ Syntax

● Structure:
○ Preprocessor Directives: #include <iostream> includes the standard
input-output stream library needed for input and output operations.
○ Namespace: using namespace std; allows the use of standard library
features without prefixing them with std::.
○ Main Function: int main() is the starting point of a C++ program. It must
return an integer, typically 0, to indicate successful execution.
○ Output Statement: cout is used for outputting text to the console. << is the
stream insertion operator.
○ Return Statement: return 0; terminates the main function and returns
control to the operating system.

cpp
Copy code
#include <iostream>
using namespace std;

int main() {
cout << "Hello, World!" << endl;
return 0;
}

Object-Oriented Programming (OOP) Concepts

What is an Object?

● Definition: An object is an instance of a class. It represents a specific example of the


class with actual values for its attributes.
● Example: If Car is a class, myCar can be an object of Car with specific attributes
like color and speed.

Classes

● Definition: A class is a blueprint for creating objects. It defines the data members
(attributes) and member functions (methods) that the objects created from the class
will have.

Syntax:
cpp
Copy code
class Car {
public:
// Attributes
string color;
int speed;

// Methods
void accelerate() {
speed += 10;
}
};


○ Attributes: Variables that store data relevant to the class. In this example,
color and speed are attributes of the Car class.
○ Methods: Functions that operate on the data contained in the class.
accelerate() is a method that modifies the speed attribute.

Methods and Messages

● Methods: Defined inside a class to perform operations on class data. They


encapsulate behavior and are often used to manipulate or access object data.

Messages: Communication between objects. This often involves invoking methods on


objects.
cpp
Copy code
Car myCar;
myCar.color = "Red";
myCar.accelerate(); // Increases speed by 10

● In this example, myCar is an object of the Car class. We set its color attribute and
call its accelerate() method.

Abstraction and Encapsulation

● Abstraction:
○ Definition: The concept of hiding the complex implementation details and
exposing only the necessary parts. It helps in managing complexity by
providing a simplified interface.
○ Example: A Car class may expose methods like drive() without exposing
internal details like how the engine works.
● Encapsulation:
○ Definition: The practice of bundling data and methods that operate on the
data into a single unit (class). It also involves restricting access to some of the
object’s components, making data hiding possible.
○ Example: Using private attributes with public getter and setter methods.

cpp
Copy code
class BankAccount {
private:
double balance; // Private attribute

public:
void deposit(double amount) {
balance += amount;
}

double getBalance() const {


return balance;
}
};

● Here, balance is private and can only be accessed or modified through the public
methods deposit() and getBalance().

Inheritance

● Definition: A mechanism where a new class (derived class) inherits attributes and
methods from an existing class (base class). It allows for hierarchical relationships
and code reusability.

Syntax:
cpp
Copy code
class Vehicle {
public:
void start() {
cout << "Vehicle started" << endl;
}
};

class Car : public Vehicle {


public:
void honk() {
cout << "Car horn honked" << endl;
}
};


○ Base Class: Vehicle class provides a general definition.
○ Derived Class: Car class inherits from Vehicle and adds its own
functionality.

Abstract Classes

● Definition: Abstract classes cannot be instantiated directly and are meant to be


inherited by other classes. They may contain one or more pure virtual functions.

Syntax:
cpp
Copy code
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};


○ Pure Virtual Function: draw() is a pure virtual function, indicated by = 0.
This makes Shape an abstract class, and it must be overridden by derived
classes.

Polymorphism

● Definition: The ability of different classes to be treated as instances of the same


class through a common interface. It allows for methods to perform different actions
based on the object’s type.
● Types:
○ Compile-time Polymorphism: Achieved through method overloading (same
method name with different parameters) and operator overloading.
○ Run-time Polymorphism: Achieved through method overriding and virtual
functions.

cpp
Copy code
class Animal {
public:
virtual void speak() {
cout << "Animal speaks" << endl;
}
};

class Dog : public Animal {


public:
void speak() override {
cout << "Dog barks" << endl;
}
};

void makeItSpeak(Animal &a) {


a.speak(); // Calls the appropriate speak() method based on the
object type
}


○ Virtual Function: speak() in Animal is a virtual function, allowing Dog to
override it. When makeItSpeak is called, it invokes the speak() method
appropriate to the object type.

You might also like