0% found this document useful (0 votes)
17 views32 pages

1 - Introduction To OOP 2024

The document provides an overview of Object-Oriented Programming (OOP) concepts, focusing on classes and objects in C++. It explains how classes serve as templates for creating objects, the inheritance of attributes and methods, and the advantages of OOP over procedural programming. Additionally, it includes examples of class and object creation, constructors, and the use of access specifiers in C++.
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)
17 views32 pages

1 - Introduction To OOP 2024

The document provides an overview of Object-Oriented Programming (OOP) concepts, focusing on classes and objects in C++. It explains how classes serve as templates for creating objects, the inheritance of attributes and methods, and the advantages of OOP over procedural programming. Additionally, it includes examples of class and object creation, constructors, and the use of access specifiers in C++.
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/ 32

OBJECT ORIENTED

PROGRAMMING
PROGRAMMING PARADIGMS -
Examples
 Monolithic – Global Data and Sequential Code (Assembly
language, BASIC)- EG input A; input B ADD A,B STORE or MOV
result to REGISTER C
 Procedural – All data is global and program mainly made up
of routines (FORTRAN, COBOL)
 Structured – Emphasis on algorithms and made up of
procedure/function with their own data (C, PASCAL)
 OOP stands for Object-Oriented Programming.
C++ What is OOP? – Classes and
objects
 In OOP a class is a template (a genre, classification or general type)
from which one or more programmable or executable sub-types can be
derived.
 A house is constructed using a building plan, the plan itself cannot be used as
accommodation. Thousands if not millions of houses can be derived from the
single building plan by sharing it online. The single building from the plan can
have unique extras in other words it can inherit or extend the features of the
original plan. The final house is an object of the building plan.
 The instances represent behaviours or modification and are called objects.
 If we created a class called Music we could have genres such as Reggae,
Jazz, Hip-Hop, Sungura etc. etc. Note that these are subclasses derived
from Music. The objects are more specific to genre e.g. a specific song…..for
one to say that they are listening to Jazz is not objective but subjective.
Music Class – Jazz subclass of Music

 These genres are in programming language class Music is called the


parent or super class whilst the others are called child or sub
classes.
 These child or sub classes are said to inherit or extend the super or
parent class’s attributes and behaviours. class. Objects or instances
of the specified class characteristics of the
 Note that there can be
C++ What is OOP? – Classes and
objects
Classes and objects are the two main aspects of object-oriented
programming.
Look at the following illustration to see the difference between
class and objects:
C++ What is OOP?

 Procedural programming is about writing procedures or functions


that perform operations on the data, while object-oriented
programming is about creating objects that contain both data and
functions.
 Object-oriented programming has several advantages over
procedural programming:
 OOP is faster and easier to execute
 OOP provides a clear structure for the programs
 OOP provides templates that allow for code reuse
C++ What is OOP?

So, a class is a template for objects, that is like a stencil or blueprint from
which an object can be built from. An object is an instance of a class. A class
brought into action. A brick is moulded using a former aka Foroma but the
building is made up of bricks not the former
When the individual objects are created, they inherit all the variables and
functions from the class.
C++ Classes and Objects Code Structure

 C++ is an object-oriented programming language.


 Everything in C++ is associated with classes and objects, along with
its attributes and methods. For example: in real life, a car is
an object. The car has attributes, such as weight and colour,
and methods, such as drive and brake.
 Attributes and methods are
basically variables and functions that belongs to the class. These
are often referred to as "class members".
 A class is a user-defined data type that we can use in our
program, and it works as an object constructor, or a "blueprint"
for creating objects.
 Your brick former or moulder the class creates or constructs brick
objects which have attributes and methods….can you list them.
Creating a Class called MyClass

class MyClass { // The name of the class

public: // Access specifier


int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};// end of class MyClass definition
Description of the class MyClass

The class keyword is used to create a class


called MyClass. Basically a class is a data-type comprising methods and
data
The public keyword is an access specifier,
which specifies that members (attributes and methods)
of the class are accessible from outside the class.
The different type of access specifiers will be covered in detail later.
Inside the class, there is an integer variable myNum
and a string variable myString. When variables are declared
within a class, they are called attributes.
The class definition ends with a semicolon ;.
Creating an Object of MyClass class

In C++, an object is created from a class. We have already created the


class named MyClass, so now we can use this to create objects. Like the
BrickFormer class can create a brick object.
To create an object of MyClass, specify the class name, followed by the
object name.
To access the class attributes (myNum and myString), use the dot syntax
(.) on the object:
Creating an Object of MyClass…
continued
 class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};

 int main() {
MyClass myObj; // Create an object of MyClass

// Access attributes and set values


myObj.myNum = 15;
myObj.myString = "Some text";
 // Print attribute values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
Creating an Object of MyClass
RESULT
Creating Multiple Objects of the same Class

 // Create a Car class with some attributes


class Car {
public:
string brand;
string model;
int year; };
int main() {
// Create an object of Car
carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
// Create another object of Car
carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
// Print attribute values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0; }
Take away

 From the same class you can generate an infinite number of objects
each with a different description derived from the initial class
definition.
 All you need to do is add more lines of code and build objects with
different descriptions.
MyCar.cpp Class code on
Code::Blocks
MyCar.cpp Class –explanation
writing the Class code
#include <iostream>
using namespace std;
class MyCar {
public:
int speed(int maxSpeed);
};
int MyCar::speed(int maxSpeed) {
return maxSpeed;
}// This is the method that returns the car
speed.
A look at main() the constructor /
driver or calling method
 int main() {
 MyCar myObj; // Create an object of
MyCar
 cout << myObj.speed(200); // Call the
method with an argument for speed
 return 0;// return of main()= 0 for no
error
 } //end main()
RESULT is the speed
Illustrating Execution Sequence
Constructor – also known as class
method
A constructor is a special method that is automatically generated by the
object of a class when it is created.

class MyConstructorExample {
public: //method is accessible from anywhere
MyConstructorExample(){
cout<< “I am the newly born MyConstructorExample.”
}//end of the constructor method
int main(){
MyConstructorExample myConstExObject; //initialises the object
return 0;}
RECALL: Creating Multiple Objects of the same Class

 // Create a Car class with some attributes


class Car {
public:
string brand;
string model;
int year; };
int main() {
// Create an object of Car
carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
// Create another object of Car
carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
// Print attribute values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0; }
Constructor with parameters

Constructors just as with using methods or regular functions, can also


take parameters or arguments. This becomes an advantage in the
setting up of initial objects already containing for attribute values. The
same class can be overloaded with have many constructors with the
same name object name but different parameters.
If we revisit the class Car that was implemented previously we could
whose attributes were brand, model and year we could reduce the
coding by create objects that access all or the specific attributes at one
time. This could be done by using the appropriate constructor
Constructor with parameters code
example
#include <iostream>
using namespace std;
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};
Constructor with parameters code
example
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);

// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\
n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\
n";
return 0;
}
Constructor with parameters code
example

int main() {
// Create Car objects and call the constructor with
different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);

// Print values
cout << carObj1.brand << " " << carObj1.model << " "
<< carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " "
<< carObj2.year << "\n";
return 0;
}
OutPut
Constructor with parameters code
example 2 a
#include <iostream>
using namespace std;
class Student { // The class
public: // Access specifier
string fName; // Attribute
string sName; // Attribute
int yearBorn; // Attribute
Student(string x, string y, int z) { // Constructor
with parameters
fName = x;
sName = y;
yearBorn = z;
}
};
Constructor with parameters code
example 2 b

int main() {
// Create Car objects and call the constructor with
different values
Student studObj1("John", "Gore", 2013);
Student studObj2("Victor", "Dhuve", 2009);

// Print values
cout << studObj1.fName << " " <<
studObj1.sName << " " << studObj1.yearBorn <<
"\n";
cout << studObj2.fName<< " " <<
studObj2.sName << " " << studObj2.yearBorn <<
"\n";
return 0;
}
Result of Constructor para meters
THE Code broken down

You might also like