0% found this document useful (0 votes)
9 views13 pages

Basic OOP

This document provides a brief overview of Object-Oriented Programming (OOP), highlighting its key concepts such as classes, objects, methods, and members. It discusses the purposes of OOP, including modularity, reusability, readability, scalability, and security, while comparing class definitions and object declarations in Python and C++. Additionally, it outlines OOP principles like composition, inheritance, encapsulation, abstraction, and polymorphism, with examples in C++.

Uploaded by

harisamser27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

Basic OOP

This document provides a brief overview of Object-Oriented Programming (OOP), highlighting its key concepts such as classes, objects, methods, and members. It discusses the purposes of OOP, including modularity, reusability, readability, scalability, and security, while comparing class definitions and object declarations in Python and C++. Additionally, it outlines OOP principles like composition, inheritance, encapsulation, abstraction, and polymorphism, with examples in C++.

Uploaded by

harisamser27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

BASIC OOP

OBJECT-ORIENTED PROGRAMMING

This Photo by Unknown Author is licensed under CC BY-SA-NC


OBJECT-ORIENTED PROGRAMMING: A
BRIEF REFRESHER
• Programming paradigm that aims to represent real-world objects more
closely
• Effectively the same as a structure
• Default access level of structures is public
• Default access level of classes is private
• Class – Blueprint/template for objects to be created in a program
• Object – Instance of a class that contains concrete data
• Member – Variable (noun) that describes the traits of a class
• Method – Function (verb) that describes the behaviors of a class
PURPOSE OF OOP
• Modularity – Breaks large programs into discrete, components
• Reusability – Code components are self-contained, independent
• Readability – Represents real-world objects in a logical fashion
• Scalability – Can be easily modified without affecting other programs
• Security – Hides internal information and enforces constraints
PYTHON CLASS VS C++ CLASS
(CLASS DEFINITION)

Python C++ (Modern)


#pragma once
class Student: //Includes
def __init__(self, name, grade):
class Student{
self.name = name public:
self.grade = grade Student(string n, double g){
name = n;
def __str__(self): grade = g;
message = f"Name: {self.name}\n" }
string toString(void){
message += f"Grade: {self.grade}" string message = "Name: " + name + "\n“ + "Grade: " +
return message to_string(grade);
return message;
}
private:
string name;
double grade;
};
PYTHON CLASS VS C++ CLASS
(OBJECT DECLARATION)

Python C++
from ClassDemo import Student #include <iostream>
#include "Student.h"
newStudent = Student("Jack Reynolds",
89.2) using namespace std;
print(newStudent)
int main(){
Student newStudent("Jack Reynolds",
89.2);
cout<< newStudent.toString();
return 0;
}
THE OLD C++ PROJECT
STRUCTURE

Class Interface File Class Implementation File


• Header file (.h) • C++ code file (.cpp)
• Serves as class declaration • Contains most or even all method
• Contains declaration of all definitions
members and methods • Each method referenced as follows
• May contain simple method • type class::method(params){}
definitions • Must include .h file
• Members and methods grouped
into private, public, or protected
WHY SEPARATE THEM?
• Artifact of C (C++ does it because C did it)
• Pros
• Modularity
• Slight increased in compilation speed
• Cons
• Complex
• Tedious
• Lessens readability
• We will use the modern structure shown in slide 5 in this course
• You may use the old structure in your pojects if you wish
OOP PRINCIPLES (PART 1)

This Photo by Unknown Author is licensed under CC BY-SA


class B {
private:
int data;
A objA;
public:

COMPOSITION B(int a){


: objA(a) {
Establishes the “has a” relationship data = a;
Complex class has an object of a simple }
class }
Object of one class serves as member void display(){
variable for another cout << “Value of class B: " << data;
Nested class definitions are allowed but cout << “Value of class A in class B: " <<
not always recommended
objA.x;
}
}; //Code source: GeeksForGeeks
class Student : private Person{
private:
char course [50];
int fee;
public:
void set_s(){
set_p();
INHERITANCE cout << "Enter the Course Name:";
Establishes an “is a” relationship cin >> course;
Child class is a derivation of its cout << "Enter the Course Fee:";
parent class cin >> fee;
C++ allows us to chose the level of }
access of inherited members and void display_s(){
methods
display_p();
cout<< "Course: " << course
<< "\nFee: " << fee << endl;
}
}; //Code source: GeeksForGeeks
class Person{
private:
string name;
int age;
public:

ENCAPSULATION Person(string n, int a){


name = n;
Binds data together with the functions that
age = a;
manipulate it
}
Protects data from unwanted use/change,
hides implementation string getName(){
Ensures objects are in control of their own
return name;
state }
Access Specifiers int getAge(){
Private – Class methods and friend functions return age;
}
Public – Entire program
}; //Code Source: GeeksForGeeks
Protected – Class methods, friend functions,
and child classes
ABSTRACTION AND
POLYMORPHISM
• Abstraction
• Hides internal implementation of certain code
• Presents an interface that is simple and easy to understand
• Polymorphism
• One object (variable, function, etc.), many forms
• Example: A parent class contains a function that performs one action, its
child class redefines said function to suit its needs

These will be covered in more detail in Part 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy