1 - Introduction To OOP 2024
1 - Introduction To OOP 2024
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
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
int main() {
MyClass myObj; // Create an object of MyClass
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
// 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