0% found this document useful (0 votes)
28 views21 pages

C++ Oop

The document discusses object-oriented programming in C++. It covers classes and objects, creating classes and objects, defining class methods, and creating multiple objects. Key topics include using classes as templates for objects, accessing object attributes and methods, and defining methods inside and outside of classes.

Uploaded by

MAHER MOHAMED
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)
28 views21 pages

C++ Oop

The document discusses object-oriented programming in C++. It covers classes and objects, creating classes and objects, defining class methods, and creating multiple objects. Key topics include using classes as templates for objects, accessing object attributes and methods, and defining methods inside and outside of classes.

Uploaded by

MAHER MOHAMED
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/ 21

Welcome

C++
By Eng.Adulahi M. Adan

http://www.mahergelle.com
Chapter Three: -
Loops, Arrays and
Functions
C++ OOP
Contents

 OOP stands for Object-Oriented Programming.


 Procedural programming is about writing procedures or
functions that perform operations on the data, while ob-
ject-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 helps to keep the C++ code DRY "Don't Repeat
Yourself", and makes the code easier to maintain, modify
and debug
C++ OOP
Contents

 OOP makes it possible to create full 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.
C++ Classes and Objects
Contents

 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++ Classes and Objects
Contents
C++ Classes and Objects
Contents

 So, a class is a template for objects, and an object is an


instance of a class.
 When the individual objects are created, they inherit all the
variables and functions from the class.
 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 color, 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".
C++ Classes and Objects
Contents

 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.
Create a Class
Contents

 To create a class, use the class keyword:


 Example Create a class called "MyClass":
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Create a Class
Contents

 Example explained
 The class keyword is used to create a class called MyClass.
 The public keyword is an access specifier, which specifies
that members (attributes and methods) of the class are
accessible from outside the class.
 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.
 At last, end the class definition with a semicolon ;.
Create an Object
Contents

 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:
Create an Object
Contents

Example // Access attributes and set values


class MyClass { // The class myObj.myNum = 15;
public: // Access specifier myObj.myString = "Some text";
int myNum;// Attribute (int variable) // Print attribute values
string myString; // Attribute (string cout << myObj.myNum << "\n";
variable) cout << myObj.myString;
}; return 0;
int main() { }
MyClass myObj; // Create an object
of MyClass
Multiple Objects
 You can create multiple objects of one class:
Example
// 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;
Multiple Objects
Contents

// Create another object of Car


Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
// Print Values to the screen
cout << carObj1.brand << " " << carObj1.model << " "
<< carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " "
<< carObj2.year << "\n";
return 0;
}
C++ Class Methods
Contents

 Methods are functions that belongs to the class.


 There are two ways to define functions that belongs to a
class:
 Inside class definition
 Outside class definition
 In the following example, we define a function inside the
class, and we name it "myMethod".
 Note: You access methods just like you access attributes;
by creating an object of the class and using the dot syntax
(.):
C++ Class Methods
Contents

Inside Example
class MyClass { // The class
public: // Access specifier
void myMethod() { // Method/function defined inside the class
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
C++ Class Methods
Contents

To define a function outside the class definition, you have to


declare it inside the class and then define it outside of the
class.
This is done by specifying the name of the class, followed the
scope resolution :: operator, followed by the name of the
function:
C++ Class Methods
Outside Example
class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};
// Method/function definition outside the class
void MyClass::myMethod() {
cout << "Hello World!";
}
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
Chapter End
Thank you
@Eng.Abdulahi Mohamed

You might also like