0% found this document useful (0 votes)
21 views7 pages

Chapter 1 C++

Uploaded by

ramsah3757
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)
21 views7 pages

Chapter 1 C++

Uploaded by

ramsah3757
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/ 7

Chapter 1.

Object-Oriented Concepts
1.1 Abstraction

• Definition: Showing only the essential details while hiding unnecessary


implementation.
• Purpose: Simplifies complexity.
• Real-world Example:
o Using a mobile phone: You press keys to dial but don’t know the internal
working of circuits.
• C++ Example:

class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() { cout << "Drawing Circle"; }
};

• Here, the abstract class hides implementation details.

1.2 Encapsulation

• Definition: Wrapping data (variables) and code (functions) into a single unit (class).
• Purpose: Data security and controlled access.
• Real-world Example: ATM machine → you interact with a secure interface.
• C++ Example:

class Account {
private:
double balance; // private data

public:
void setBalance(double b) { balance = b; }
double getBalance() { return balance; }
};
1.3 Inheritance

• Definition: Deriving a new class from an existing one.


• Purpose: Code reuse.
• Types of Inheritance in C++:
o Single
o Multiple
o Multilevel
o Hierarchical
o Hybrid
• Example:

class Animal {
public:
void eat() { cout << "Eating..."; }
};

class Dog : public Animal {


public:
void bark() { cout << "Barking..."; }
};

int main() {
Dog d;
d.eat();
d.bark();
}

1.4 Polymorphism

• Definition: “Many forms” → same function behaves differently.

Types:

1. Compile-time (Static) → Function overloading, Operator overloading.


class Print {
public:
void show(int x) { cout << "Int: " << x; }
void show(double y) { cout << "Double: " << y; }
};
2. Run-time (Dynamic) → Achieved using virtual functions.

class Animal {
public: virtual void sound() { cout << "Animal Sound"; }
};

class Dog : public Animal {


public: void sound() { cout << "Bark"; }
};

int main() {
Animal* a;
Dog d;
a = &d;
a->sound(); // Bark
}

1.5 Advantages of OOP

• Reusability (via inheritance).


• Security (via encapsulation).
• Flexibility & maintainability.
• Easy debugging.
• Simulates real-world entities

1.6 Applications of OOP

• Banking Systems – handling accounts, loans.


• Gaming – objects like players, weapons.
• Simulation software – flight simulator, hospital mgmt.
• GUI applications – Windows, Android apps.
• Object-oriented databases.

2. Introduction to C++
2.1 Data Types

Type Example Description


int 10 Integer values
float 3.14 Single precision decimal
double 3.14159 Double precision decimal
char 'A' Single character
bool true/false Boolean values
string "Hello" Sequence of characters
2.2 new Operator and Keywords

• new → dynamic memory allocation.


• delete → free memory.

int* p = new int(25);

cout << *p;

delete p;

• Some C++ keywords: class, public, private, virtual, friend, namespace, this, new, delete.

2.3 Namespace Concept

• Prevents name conflicts.


• Example:

namespace A { int x = 10; }

namespace B { int x = 20; }

int main() {

cout << A::x;

cout << B::x;

2.4 Input & Output

• cin → input
• cout → output

int a;

cin >> a;

cout << "Value = " << a;


2.5 Simple C++ Program

#include <iostream>

using namespace std;

int main() {

cout << "Welcome to C++!";

return 0;

2.6 Reference Variables

• Another name for an existing variable.

int a = 5;

int &ref = a;

ref = 10; // a also becomes 10

2.7 Classes and Objects

• Class: User-defined type.


• Object: Instance of class.

class Student {

public:

int roll;

string name;

void display() { cout << roll << " " << name; }

};

int main() {
Student s1;

s1.roll = 1; s1.name = "Amit";

s1.display(); }
2.8 this Pointer

• Points to the current object.

class Test {

int x;

public:

void setX(int x)

this->x = x;

};

2.9 Access Specifiers

Specifier Access
public Anywhere
private Within class only
protected Within class & derived classes

2.10 Defining Members

1. Inside class

class Demo {

int x;

public:

void setX(int a) { x = a; }

};
2. Outside class

class Demo

int x;

public:

void setX(int a);

};

void Demo::setX(int a)

x = a;

You might also like