OOP
OOP
C++ What is OOP?
OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the
code easier to maintain, modify and debug
OOP makes it possible to create fully reusable applications with less code and
shorter development time
Tip: The "Don't Repeat Yourself" (DRY) principle is about reducing the
repetition of code. You should extract out the codes that are common for the
application, and place them at a single place and reuse them instead of
repeating it.
The foregoing shows the concept of the reuse of both inbuilt libraries and
user archives that can also be made public and shared over the network.
Nowadays the majority of apps and code are shared over the internet by
organisations and individuals.
C++ What is OOP – Class v Object(s)
Example Illustration
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: Class v Object(s) Example
Illustration cont’d
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 make model, year
maxSpeed, weight and colour.
The car has or exhibits behaviours such as drive, stop , turn , hoot ,
speedup , slowdown and many others. These behaviours are, in
programming terms, called methods or functions. These functions
can be called or invoked from anywhere within the class or from
another class (subject to permissions being provided)
C++ Classes and Objects Code Structure
cont’d
These functions are defined during the program coding with or
without parameters and are expected to respond by obeying the order
and responding with acknowledgement of having carried out the task
in form of some value or a message that the caller can understand,
The return or response token message provides the calling program
with the results in the form of some numerical value or message
indicating success failure or an error.
ABSTRACT DATA Type model
This models the class as a complex and abstract data type in form of a
bundle of both data and functions/methods where the later acts on the data.
The data and the methods may also be accessed from other classes subject to
permissions being granted eg by subclasses if they have protected level of
access and by all other classes if they have a public access level.
An object when makes reference or points to the model
C++ Classes and Objects Code Structure
Attributes and methods are basically variables and functions that
belong 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(building/constructing) objects.
Our brickformer 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 the 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. 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:
MyClass ObjectOfMyClass; declares ObjectOfMyClass as an object of
MyClass.
In other words ObjectOfMyClass makes reference to an instance of the
class MyClass. The object of MyClass is then able to access the
accessable member variables and functions of the class attributes
and
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
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
// Create another object of Car
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
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
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
// Create another object of Car
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 – 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;}
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