KEMBAR78
Sec 04 Object-Oriented Programming OOP (Class).pdf
Class
Data Structure Sec 04
Prepared by: Abdullah Emam
Fayoum international Technological University, Department of
Information Technology
What is Object-Oriented Programming?
•OOP is a programming paradigm based on objects and classes.
•It provides a way to structure programs using real-world entities.
•Key concepts:
Encapsulation
Inheritance
Polymorphism
Abstraction
What is a Class?
 A class is a blueprint for creating objects.
 It encapsulates data (variables) and functions (methods).
class Car {
public:
string brand;
int speed;
void display() {
cout << "Brand: " << brand << ", Speed: " << speed <<
" km/h" << endl;
}
};
What is an Object?
 An object is an instance of a class.
 Each object has its own values for the class attributes.
Car car1; // Object creation
car1.brand = "Toyota";
car1.speed = 120;
car1.display();
Declaring a Class in C++
A class is defined using the class keyword.
Example:
class Student {
public:
string name;
int age;
};
Access Specifiers
 Public: Accessible from anywhere.
 Private: Accessible only inside the class.
 Protected: Accessible within the class and derived classes.
class Example {
private:
int secret;
public:
int number;
};
Creating Objects
Student s1;
s1.name = "Alice";
s1.age = 20;
Using Member Functions
 Functions inside the class operate on its data.
class Student {
public:
string name;
int age;
void display() {
cout << "Name: " << name << ", Age:
" << age << endl;
}
};
Constructors
 Constructor initializes objects automatically.
class Student {
public:
string name;
int age;
Student() {
name = "Unknown";
age = 0;
}
};
Parameterized Constructor
 Accepts arguments for initialization.
Student(string n, int a) {
name = n;
age = a;
}
Destructor
 Destructor is called when an object is deleted.
~Student() {
cout << "Destructor
called!" << endl;
}
Encapsulation
Encapsulation protects data by making
variables private and using public
methods.
class Person {
private:
int age;
public:
void setAge(int a) { age = a; }
int getAge() { return age; }
};
Getters and Setters
p.setAge(25);
cout << p.getAge();
Inheritance
 One class (child) derives properties from another (parent).
class Animal {
public:
string name;
};
class Dog : public Animal {
public:
void bark() {
cout << "Woof! Woof!" << endl;
}
};
Exercise 1: Creating a Class and Objects
Problem:
Create a class Book with attributes title, author, and
price. Create objects and display their details using a
function.
#include <iostream>
using namespace std;
class Book {
public:
string title;
string author;
float price;
void display() {
cout << "Title: " << title << ", Author: " << author << ", Price: $" <<
price << endl;
}
};
int main() {
Book b1;
b1.title = "C++ Programming";
b1.author = "Bjarne Stroustrup";
b1.price = 49.99;
b1.display();
return 0;
}
Exercise 2: Encapsulation (Private Members with
Getters and Setters)
Problem:
Create a class BankAccount with private attributes
accountNumber and balance. Provide public getters and
setters for modifying balance safely.
#include <iostream>
using namespace std;
class BankAccount {
private:
int accountNumber;
double balance;
public:
BankAccount(int accNum, double bal) {
accountNumber = accNum;
balance = bal;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance)
balance -= amount;
else
cout << "Insufficient balance!" << endl;
}
double getBalance() {
return balance;}
};
int main() {
BankAccount account(12345, 500.0);
account.deposit(200);
account.withdraw(100);
cout << "Remaining Balance: $" <<
account.getBalance() << endl;
return 0;
}
Exercise 3: Constructor and Parameterized
Constructor
Problem:
Create a Student class with attributes name and
age. Use a parameterized constructor to initialize
values.
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
Student(string n, int a) { // Parameterized constructor
name = n;
age = a;
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
Student s1("Alice", 20);
Student s2("Bob", 22);
s1.display();
s2.display();
return 0;
}
Exercise 4: Encapsulation and Access Specifiers
Problem:
Create a class Car with private attributes brand and
price. Use public methods to set and get these
values.
class Car {
private:
string brand;
double price;
public:
void setDetails(string b, double p) {
brand = b;
price = p;
}
void display() {
cout << "Car Brand: " << brand << ", Price: $" << price <<
endl;
}
};
int main() {
Car car1;
car1.setDetails("Tesla", 75000.0);
car1.display();
return 0;
}
Sec 04 Object-Oriented Programming OOP (Class).pdf

Sec 04 Object-Oriented Programming OOP (Class).pdf

  • 1.
    Class Data Structure Sec04 Prepared by: Abdullah Emam Fayoum international Technological University, Department of Information Technology
  • 2.
    What is Object-OrientedProgramming? •OOP is a programming paradigm based on objects and classes. •It provides a way to structure programs using real-world entities. •Key concepts: Encapsulation Inheritance Polymorphism Abstraction
  • 3.
    What is aClass?  A class is a blueprint for creating objects.  It encapsulates data (variables) and functions (methods). class Car { public: string brand; int speed; void display() { cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl; } };
  • 4.
    What is anObject?  An object is an instance of a class.  Each object has its own values for the class attributes. Car car1; // Object creation car1.brand = "Toyota"; car1.speed = 120; car1.display();
  • 5.
    Declaring a Classin C++ A class is defined using the class keyword. Example: class Student { public: string name; int age; };
  • 6.
    Access Specifiers  Public:Accessible from anywhere.  Private: Accessible only inside the class.  Protected: Accessible within the class and derived classes. class Example { private: int secret; public: int number; };
  • 7.
    Creating Objects Student s1; s1.name= "Alice"; s1.age = 20;
  • 8.
    Using Member Functions Functions inside the class operate on its data. class Student { public: string name; int age; void display() { cout << "Name: " << name << ", Age: " << age << endl; } };
  • 9.
    Constructors  Constructor initializesobjects automatically. class Student { public: string name; int age; Student() { name = "Unknown"; age = 0; } };
  • 10.
    Parameterized Constructor  Acceptsarguments for initialization. Student(string n, int a) { name = n; age = a; }
  • 11.
    Destructor  Destructor iscalled when an object is deleted. ~Student() { cout << "Destructor called!" << endl; }
  • 12.
    Encapsulation Encapsulation protects databy making variables private and using public methods. class Person { private: int age; public: void setAge(int a) { age = a; } int getAge() { return age; } };
  • 13.
  • 14.
    Inheritance  One class(child) derives properties from another (parent). class Animal { public: string name; }; class Dog : public Animal { public: void bark() { cout << "Woof! Woof!" << endl; } };
  • 15.
    Exercise 1: Creatinga Class and Objects Problem: Create a class Book with attributes title, author, and price. Create objects and display their details using a function.
  • 16.
    #include <iostream> using namespacestd; class Book { public: string title; string author; float price; void display() { cout << "Title: " << title << ", Author: " << author << ", Price: $" << price << endl; } }; int main() { Book b1; b1.title = "C++ Programming"; b1.author = "Bjarne Stroustrup"; b1.price = 49.99; b1.display(); return 0; }
  • 17.
    Exercise 2: Encapsulation(Private Members with Getters and Setters) Problem: Create a class BankAccount with private attributes accountNumber and balance. Provide public getters and setters for modifying balance safely.
  • 18.
    #include <iostream> using namespacestd; class BankAccount { private: int accountNumber; double balance; public: BankAccount(int accNum, double bal) { accountNumber = accNum; balance = bal; } void deposit(double amount) { balance += amount; } void withdraw(double amount) { if (amount <= balance) balance -= amount; else cout << "Insufficient balance!" << endl; } double getBalance() { return balance;} }; int main() { BankAccount account(12345, 500.0); account.deposit(200); account.withdraw(100); cout << "Remaining Balance: $" << account.getBalance() << endl; return 0; }
  • 19.
    Exercise 3: Constructorand Parameterized Constructor Problem: Create a Student class with attributes name and age. Use a parameterized constructor to initialize values.
  • 20.
    #include <iostream> using namespacestd; class Student { public: string name; int age; Student(string n, int a) { // Parameterized constructor name = n; age = a; } void display() { cout << "Name: " << name << ", Age: " << age << endl; } }; int main() { Student s1("Alice", 20); Student s2("Bob", 22); s1.display(); s2.display(); return 0; }
  • 21.
    Exercise 4: Encapsulationand Access Specifiers Problem: Create a class Car with private attributes brand and price. Use public methods to set and get these values.
  • 22.
    class Car { private: stringbrand; double price; public: void setDetails(string b, double p) { brand = b; price = p; } void display() { cout << "Car Brand: " << brand << ", Price: $" << price << endl; } }; int main() { Car car1; car1.setDetails("Tesla", 75000.0); car1.display(); return 0; }