SlideShare a Scribd company logo
Object-Oriented Programming.
Class and Object
Creating Objects
Class Methods
Definition:
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.
Advantages:
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
OOP makes it possible to create full
reusable applications with less code and
shorter development time
Classes/Objects
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".
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
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)
};
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. You will
learn more about access specifier later.
Example explained
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
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:
Example
Create an object called "myObj" and
access the attributes:
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;
}
Class Methods
Methods are functions that belongs to
the class.
There are two ways to define functions
that belongs to a class:
1) Inside class definition
2) Outside class definition
In the following example, we define a
function inside the class, and we name it
"myMethod".
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;}
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:
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;
}

More Related Content

PPT
C++ constructors
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPT
Class and object in c++
PPTX
Write First C++ class
PPTX
[OOP - Lec 20,21] Inheritance
PPTX
Object Oriented Programming_Lecture 2
PPTX
[OOP - Lec 19] Static Member Functions
PPTX
Chapter2 array of objects
C++ constructors
[OOP - Lec 09,10,11] Class Members & their Accessing
Class and object in c++
Write First C++ class
[OOP - Lec 20,21] Inheritance
Object Oriented Programming_Lecture 2
[OOP - Lec 19] Static Member Functions
Chapter2 array of objects

What's hot (19)

PPTX
[OOP - Lec 18] Static Data Member
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
Constructor in java
PPT
Classes and objects
PPTX
Class and objects
PPT
Object and class presentation
PPTX
PPT
Data members and member functions
PPTX
Classes and objects
PPTX
Inner classes in java
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
PPTX
Presentation on class and object in Object Oriented programming.
PPT
Classes and objects
PPTX
‫Object Oriented Programming_Lecture 3
PPTX
Constructor overloading & method overloading
PPT
java
PPT
2 lesson 2 object oriented programming in c++
PPTX
Classes and objects in c++
[OOP - Lec 18] Static Data Member
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Constructor in java
Classes and objects
Class and objects
Object and class presentation
Data members and member functions
Classes and objects
Inner classes in java
[OOP - Lec 04,05] Basic Building Blocks of OOP
Presentation on class and object in Object Oriented programming.
Classes and objects
‫Object Oriented Programming_Lecture 3
Constructor overloading & method overloading
java
2 lesson 2 object oriented programming in c++
Classes and objects in c++
Ad

Similar to creating objects and Class methods (20)

DOCX
OOP and C++Classes
PPTX
OOP in C# Classes and Objects.
PPTX
Object Oriented Programming Class and Objects
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
PDF
Intro to iOS Development • Made by Many
PDF
Lecture2.pdf
PPTX
classes object fgfhdfgfdgfgfgfgfdoop.pptx
PPTX
OOPs & C++ UNIT 3
PPTX
Classes and Objects in C#
PPTX
Unit – V Object Oriented Programming in Python.pptx
PPTX
13_User_Defined_Objects.pptx objects in javascript
PPT
Unidad o informatica en ingles
PPTX
OOC MODULE1.pptx
PPT
PPTX
Lecture 4. mte 407
PDF
OOPs Interview Questions PDF By ScholarHat
PPT
Objective c
PPTX
Object Oriended Programming with Java
PPTX
object oriented programing lecture 1
OOP and C++Classes
OOP in C# Classes and Objects.
Object Oriented Programming Class and Objects
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
Intro to iOS Development • Made by Many
Lecture2.pdf
classes object fgfhdfgfdgfgfgfgfdoop.pptx
OOPs & C++ UNIT 3
Classes and Objects in C#
Unit – V Object Oriented Programming in Python.pptx
13_User_Defined_Objects.pptx objects in javascript
Unidad o informatica en ingles
OOC MODULE1.pptx
Lecture 4. mte 407
OOPs Interview Questions PDF By ScholarHat
Objective c
Object Oriended Programming with Java
object oriented programing lecture 1
Ad

Recently uploaded (20)

PDF
Sports Quiz easy sports quiz sports quiz
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Pre independence Education in Inndia.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Institutional Correction lecture only . . .
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Cell Types and Its function , kingdom of life
PDF
Classroom Observation Tools for Teachers
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
01-Introduction-to-Information-Management.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
master seminar digital applications in india
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Sports Quiz easy sports quiz sports quiz
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pre independence Education in Inndia.pdf
Complications of Minimal Access Surgery at WLH
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Institutional Correction lecture only . . .
GDM (1) (1).pptx small presentation for students
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Cell Types and Its function , kingdom of life
Classroom Observation Tools for Teachers
Renaissance Architecture: A Journey from Faith to Humanism
01-Introduction-to-Information-Management.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
master seminar digital applications in india
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx

creating objects and Class methods

  • 1. Object-Oriented Programming. Class and Object Creating Objects Class Methods
  • 2. Definition: 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.
  • 3. Advantages: 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 OOP makes it possible to create full reusable applications with less code and shorter development time
  • 4. Classes/Objects 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.
  • 5. 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.
  • 6. Create a Class 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) };
  • 7. 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. You will learn more about access specifier later.
  • 8. Example explained 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 ;.
  • 9. Create an Object 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:
  • 10. Example Create an object called "myObj" and access the attributes: class MyClass { // The class public: // Access specifier int myNum; // Attribute (int variable) string myString; // Attribute (string variable) };
  • 11. 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; }
  • 12. Class Methods Methods are functions that belongs to the class. There are two ways to define functions that belongs to a class: 1) Inside class definition 2) Outside class definition In the following example, we define a function inside the class, and we name it "myMethod".
  • 13. 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;}
  • 14. 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:
  • 15. 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!"; }
  • 16. int main() { MyClass myObj; // Create an object of MyClass myObj.myMethod(); // Call the method return 0; }