Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views34 pages

Oop in C++ Introduction

This is an oop in c++ introduction

Uploaded by

devkotabhim83
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)
7 views34 pages

Oop in C++ Introduction

This is an oop in c++ introduction

Uploaded by

devkotabhim83
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/ 34

CHAPTER -1

Evolution of C++
1. C Language (1972)
• Developed by Dennis Ritchie.
• Procedural programming language.
• Focused on structured programming and low-level memory
access.
• Used to build operating systems like UNIX.

2. C with Classes (1979)


• Introduced by Bjarne Stroustrup at Bell Labs.
• He extended C to support classes, a key concept in object-
oriented programming.
• It added:
• Classes & Objects
• Constructors / Destructors
• Basic Inheritance
3. C++ Language (1983)
• "C with Classes" officially became C++.
• Name "C++" is a play on C’s increment operator ++,
meaning “C improved”.
• Added features:
• Encapsulation, Inheritance, Polymorphism
• Function Overloading
• Operator Overloading
4. C++98 / C++03 (Standardization)
• C++98 (1998): First official ISO/ANSI standard.
• Added:
• Templates
• STL (Standard Template Library) – vectors, maps, etc.
• C++03 (2003): Small bug fixes and improvements.

5. C++11 (Modern C++)


• Released in 2011 with big upgrades:
• Auto keyword
• Lambda functions
• Smart pointers
• Range-based for loops
• Move semantics
6. C++14 / C++17
• C++14: Minor improvements and bug fixes.
• C++17: Added:
• Structured bindings
• If with initializer
• Filesystem library
7. C++20 / C++23
• C++20: Major update with:
• Concepts
• Co routines
• Modules
• Ranges
Differences between C and C++
Differences between OOP and POP
OOP (Object-Oriented POP (Procedure-Oriented
Programming) Programming)
Based on objects and classes Based on functions and procedures
Focuses on data (objects) Focuses on functions (procedures)
Encapsulation hides data (more Data is global and can be accessed
secure) from anywhere
Supports inheritance and Does not support inheritance or
polymorphism polymorphism
Code reuse and maintenance is
Easy to reuse and maintain code
harder
Models real-world problems
Difficult to model real-world problems
effectively
Program is divided into objects Program is divided into functions
Examples: C++, Java, Python (OOP) Examples: C, Pascal, BASIC
Better for large and complex
Better for small and simple programs
applications
Preprocessor directives
• Preprocessor directives are special instructions in a program that are
processed before the actual compilation begins.

They tell the preprocessor to:

• Include files (like libraries).


• Define macros (constants or functions).
• Conditionally compile code.
• Avoid duplicate inclusions.

Directive Description

#include Includes contents of another file (e.g., headers).


#define Defines constants or macros.
#undef Undefines a macro.
#ifdef If macro is defined, compile the block.
Header File in C++

• A header file in C++ is a file that usually contains function


declarations, class definitions, macros, or constants—
things you want to share across multiple files.

#include <filename>

Why Use Header Files?


• Code Reusability – Write once, use in many .cpp files.
• Organization – Keeps declarations separate from
implementations.
• Simplifies Maintenance – You change the declaration in one
place.
Variable

• A variable is a name that stores a value in memory.


It allows your program to store and use data like
numbers, character

datatype variable_name = value;

• int age = 24;


• float pi = 3.14;
• string name = “Ram”
🔹 Basic Syntax

#include <iostream>
using namespace std;

int main() {
// code goes here
return 0;
}
Basic Input/Output Operations in C++

• In C++, input/output (I/O) operations are done using:


• cin → for input (from keyboard)
• cout → for output (to screen)
• These are part of the iostream library.
Example 1 :

#include <iostream>
using namespace std;

int main() {
int number;

// Input
cout << "Enter a number: ";
cin >> number;

// Output
cout << "You entered: " << number << endl;

return 0;
}
Example 2 : with Multiple Variables

#include <iostream>
using namespace std;

int main() {
int a, b;

cout << "Enter two numbers: ";


cin >> a >> b;

cout << "Sum is: " << a + b << endl;

return 0;
}
Using getline() for Full Line Input
#include <iostream>
#include <string> // Needed for getline
using namespace std;

int main() {
string fullName;

cout << "Enter your full name: ";


getline(cin, fullName); // Takes full line

cout << "Welcome, " << fullName << "!" << endl;

return 0;
}
Classes & Objects
1. Class
• A class is like a blueprint for creating objects.
• It defines properties (attributes & data) and behaviors (functions
and methods)of an object.

Example 1 :

class Car
{
public:
string brand;
void drive()
{
cout << "Driving";
}
};
Example : 2
class Student
{
public:
string name;
int age;
void study()
{
cout << name << " is studying at age " << age << "."
<< endl;
}
};
Example 3:

#include <iostream>
using namespace std;

class BankAccount
{
public:
string holderName;
int balance;

void showBalance()
{
cout << holderName << " has $" << balance << " in the account." <<
endl;
}
};
2. Object
• An object is a real-world instance of a class.
• You can create many objects from one class.

Example 1 :

Car myCar;
myCar.brand = "Toyota";
myCar.drive(); // Output: Driving
Example 2:

Student s1; // Object


s1.name = "John";
s1.age = 20;
s1.study();

Example 3:

BankAccount acc1; // Object


acc1.holderName = "Alice";
acc1.balance = 5000;
acc1.showBalance();
Example of classes & Object
Example 1- Car:
#include <iostream>
using namespace std;
Output:
class Car
{
public: Toyota is driving at 80 km/h.
string brand;
int speed;

void drive()
{
cout << brand << " is driving at " << speed << " km/h." << endl;
}
};

int main()
{
Car car1; // Object
car1.brand = "Toyota";
car1.speed = 80;
car1.drive();

return 0;
}
Example 2: Class – Student

#include <iostream>
using namespace std; Output:
class Student {
public: John is studying at age
string name; 20.
int age;

void study() {
cout << name << " is studying at age " << age << "." << endl;
}
};

int main() {
Student s1; // Object
s1.name = "John";
s1.age = 20;
s1.study();

return 0;
}
Example 3: Class – BankAccount

#include <iostream> Output:


using namespace std;
Alice has $5000 in the
class BankAccount {
public:
account.
string holderName;
int balance;

void showBalance() {
cout << holderName << " has $" << balance << " in the account." << endl;
}
};

int main() {
BankAccount acc1; // Object
acc1.holderName = "Alice";
acc1.balance = 5000;
acc1.showBalance();

return 0;
}
Escape Sequences in C++
Definition:
• Escape sequences are special characters in C++ that begin with a
backslash (\) and are used to represent things like new lines, tabs,
quotes, etc., that can’t be typed directly.
• They are mostly used in strings or characters to control output
formatting.

Common Escape Sequences

Escape Sequence Meaning Example Output


\n New line Moves to the next line
\t Horizontal tab Adds a tab space
Prints a single backslash
\\ Backslash
\
What is a Comment?
• A comment is a piece of text in a program that is ignored by the compiler.
• It is used to write notes, explanations, or reminders inside the code.
• Comments help programmers understand the code better, especially when
revisiting it later or sharing with others.
• Comments do not affect how the program runs.

Types of Comments in C++

• Single-line comment — starts with // and continues to the end of the line.
// This is a single-line comment
cout << "Hello"; // This comment is after code

• Multi-line comment — starts with /* and ends with */, can span multiple lines.
/* This is a multi-line
comment */
cout << "World";
Abstraction
Abstraction means showing only the important details
and hiding the internal working of an object from the
user.

Example:
When you drive a car, you:
Use the steering, accelerator, and brake (these are the
important things shown),
But you don't see how the engine, fuel injection, or brake
system works (internal details are hidden).
This is abstraction.
Why Use Abstraction?

• To hide complexity.
• To focus on what an object does, not how it does it.
• To make code cleaner, simpler, and more secure.

Abstraction using Abstract Class

Syntax :

class Animal {
public:
virtual void sound() = 0; // Pure virtual function
};

class Dog : public Animal {


public:
void sound() {
cout << "Bark" << endl;
}
};
Encapsulation
• Encapsulation is the process of wrapping of data and
methods in a single unit.
• with data encapsulation, data is not accessible to the outside
world only the functions that are wrapped in the class can
access them.
• normally we keep data in private section and function in
public section.

Access specifier / visibility specifier


• Private
• Public
• Protected
Example:

class Student
{
private:
string name;
int age;

public:
void setData()
{
name="Sujan";
age=50;
salary=50;
}
void showData()
{
cout<<"The name is "<<name<<"."<<" The age is "<<age<<"
and salary is "<<salary<<endl;
}
protected:
int salary;
};
Inheritance
What is Inheritance?
• Inheritance is a feature of OOP (Object-Oriented
Programming) where one class (child) can reuse the
properties and behavior (variables and functions) of
another class (parent).

Example:
• A son inherits traits (like surname) from his father.
In C++:
• Parent class = Base class
• Child class = Derived class
Syntax:
class Base {
// base class code
};

class Derived : access-specifier Base {


// derived class code
};

Example:
class Animal { //baseclass
public:
void sound() {
cout << “Animal makes sound" << endl;
}
};

class Dog : public Animal { //derived class


public:
void bark() {
cout << “Dog can bark" << endl;
}
};
Polymorphism
What is Polymorphism?
• Polymorphism means one function behaving differently in
different situations.
It comes from Greek:
• Poly = many
• Morph = forms
So, Polymorphism = many forms

Example:
• A person acts differently:
• At home → son
• At school → student
• On the field → player
• Same person (same name) — different behavior. That’s
polymorphism.
Types of Polymorphism

Type Description
Compile-time Decided by the compiler
Run-time Decided while the program is running
THANK YOU

You might also like