Mada za sehemu hiiDemonstrate mastery of basic principles of Object Oriented Programming (Using C++; Java; Python; etc)Mada 4
- Describe the concept of Object Oriented Programming (Output, Directives, input, type bool, set width manipulator, type conversion, Object oriented paradigm differences between Object Oriented Programming and Procedure oriented programming, Encapsulation, Inheritance, composition and Polymorphism, Benefits of OOP, Structure of C++/Java/python, namespace, Data types, C++/Java/python tokens, Identifiers, Variables, Constants, Operators, Control structures and Loops)
- Describe the general structure of Object Oriented Program (Using C++, Java, Python, etc)
- Apply appropriate syntax and constructs to create Object Oriented programs
- Debug Object Oriented programs using appropriate skills (Use C++, Java, Python, etc)
Object Oriented Programming Structure
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data and objects rather than functions and logic. In OOP, programs are built from objects that contain data (attributes) and code (methods), making the code modular, reusable, and easier to maintain.
Object-oriented programming (OOP) is a programming concept that revolves around the idea of "objects." These objects hold data as fields (also called attributes or properties) and code as procedures (also called methods). OOP enables developers to build programs in a modular way and create code that can be reused.
In OOP, objects are created from classes, which act as templates for creating objects. Classes specify the characteristics and actions that objects of that class will possess.
Key OOP Terminology
| Term | Definition |
|---|---|
| Class | A user-defined data type that acts as a blueprint for creating individual objects |
| Object | An instance of a class; represents a specific entity with its own unique data |
| Attributes | Characteristics or properties of an object (data stored in variables) |
| Methods | Functions defined inside a class that describe the behaviors of an object |
For example, a class named "Car" serves as a blueprint. From this class, we can create specific objects like "Toyota" or "Nissan." Each object has attributes (such as color, model, year) and methods (such as startEngine(), accelerate(), brake()).

An OOP program consists of the following structural components:
1. Class Definition
A class is the fundamental building block of any OOP program. It defines the structure and behavior that objects of that class will have.
General structure of a class:
class ClassName {
// Access specifier
public:
// Public members (attributes and methods)
private:
// Private members (attributes and methods)
protected:
// Protected members
};
2. Attributes (Data Members)
Attributes are variables declared within a class that store the state or data of objects. They represent the characteristics of the object.
class Car {
private:
string color; // Attribute
string model; // Attribute
int year; // Attribute
int speed; // Attribute
};
3. Methods (Member Functions)
Methods are functions defined inside a class that perform operations on objects. They define the behaviors or actions that objects can perform.
class Car {
public:
void startEngine() {
// Method implementation
}
void accelerate() {
// Method implementation
}
void brake() {
// Method implementation
}
};
4. Constructors and Destructors
- Constructor: A special method called when an object is created; used to initialize object attributes
- Destructor: A special method called when an object is destroyed; used to clean up resources
5. Access Specifiers
Access specifiers control the visibility and accessibility of class members:
| Specifier | Description |
|---|---|
| public | Members accessible from anywhere |
| private | Members accessible only within the class |
| protected | Members accessible within the class and derived classes |
Objects are instances of a class. The process involves:
- Defining a class (the blueprint)
- Instantiating an object (creating an instance)
- Accessing members through the object
// Creating an object
Car myCar; // Declaration
myCar.startEngine(); // Calling a method
Encapsulation
Encapsulation is the practice of bundling data (attributes) and methods that operate on that data within a single unit (class). It also involves hiding internal details and providing a public interface.
Benefits:
- Data protection: Only authorized methods can access or modify data
- Controlled modifications: Methods enforce rules regarding data changes
- Information hiding: Internal implementation details are hidden
Inheritance
Inheritance enables a new class (subclass) to acquire attributes and methods from an existing class (superclass). This promotes code reuse.
class Animal { // Parent class
public:
void eat() { }
};
class Dog : public Animal { // Child class inherits from Animal
public:
void bark() { }
};
Polymorphism
Polymorphism allows objects from different classes to be treated as objects of a common superclass. A single method name can perform different actions depending on the object type.
Abstraction
Abstraction focuses on what an object does rather than how it does it. It hides complex implementation details and shows only essential features.
Consider a school library system:
Step 1: Identify classes
- Book
- Student
- Librarian
Step 2: Define the Book class
class Book {
private:
string title;
string author;
string ISBN;
int availableCopies;
public:
// Constructor
Book(string t, string a, string i, int copies) {
title = t;
author = a;
ISBN = i;
availableCopies = copies;
}
// Methods
void displayDetails() {
cout << "Title: " << title << endl;
cout << "Author: " << author << endl;
}
bool checkout() {
if (availableCopies > 0) {
availableCopies--;
return true;
}
return false;
}
};
Step 3: Create objects
int main() {
Book book1("Harry Potter", "J.K. Rowling", "978-0", 5);
book1.displayDetails();
if (book1.checkout()) {
cout << "Book checked out successfully!" << endl;
}
return 0;
}
C++ Structure
class ClassName {
public:
int attribute;
void method() { }
};
Java Structure
public class ClassName {
private int attribute;
public void method() { }
}
Python Structure
class ClassName:
def __init__(self):
self.attribute = 0
def method(self):
pass
In Tanzania, OOP structure is used in mobile banking applications like M-Pesa or banking systems. For example, a bank account system in Python would define a BankAccount class with attributes like account number, balance, and owner name, along with methods for deposit and withdrawal. When a customer creates an account, an object is instantiated from this class, and each customer's data is encapsulated, protected, and managed separately—similar to how school fee payment systems at schools like St. Francis Girls or Makerere University process individual student accounts securely.
Swali
Which of the following represents the general structure of a class in Object Oriented Programming?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuuliza