Mod 2 C++
Mod 2 C++
● 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.
● 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;
}
What is an Object?
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.
● In this example, myCar is an object of the Car class. We set its color attribute and
call its accelerate() method.
● 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;
}
● 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;
}
};
●
○ Base Class: Vehicle class provides a general definition.
○ Derived Class: Car class inherits from Vehicle and adds its own
functionality.
Abstract Classes
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
cpp
Copy code
class Animal {
public:
virtual void speak() {
cout << "Animal speaks" << endl;
}
};
●
○ 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.