Objects and Classes
Introduction to Programming
Compiled by
G. Pachshenko
Week 15
Lecture 15
C++ is a multi-paradigm programming language.
Meaning, it supports different programming
styles.
One of the popular ways to solve a programming
problem is by creating objects, known as object-
oriented style of programming.
C++ supports object-oriented (OO) style of
programming which allows you to divide
complex problems into smaller sets by creating
objects.
Object is a collection of data and functions that
act on those data.
Before you create an object in C++, you need to
define a class.
A class is a blueprint for the object.
We can think of class as a sketch (prototype) of a
house. It contains all the details about the floors,
doors, windows etc.
Based on these descriptions we build the house.
House is the object.
As, many houses can be made from the same
description, we can create many objects from a
class.
A class is defined in C++ using
keyword class followed by the name of class.
The body of class is defined inside the curly
brackets and terminated by a semicolon at the
end.
class className
{
// some data
// some functions
};
Example
Create a class called “MyClass”
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Create an object called “myObj” and access
the attributes:
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:
• 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 by using the dot
syntax (.)
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;
}
Example: Class in C++:
class Test
{
private:
int data1;
float data2;
public:
void function1()
{ data1 = 3; }
float function2()
{
data2 = 4.7;
return data2;
}
}
We defined a class named Test.
This class has two
data members: data1 and data2
and two member functions:
function1() and function2().
Keywords: private and public.
The private keyword makes data and functions
private. Private data and functions can be
accessed only from inside the same class.
The public keyword makes data and functions
public.
Public data and functions can be accessed out of
the class.
Here, data1 and data2 are private members
where as function1() and function2() are public
members.
If you try to access private data from outside of
the class, compiler throws error.
This feature in OOP is known as data hiding.
When class is defined, only the specification for
the object is defined; no memory or storage is
allocated.
To use the data and access functions defined in
the class, you need to create objects.
Syntax to Define Object in C++:
className objectVariableName;
You can create objects of Test class (defined in
above example) as follows:
int main()
{
Test o1, o2;
}
//Here, two objects o1 and o2 of Test class are
created.
class Test
{
private:
int data1;
float data2;
public:
void function1()
{ data1 = 2; }
float function2()
{
data2 = 3.5;
return data2;
}
};
int main()
{
Test o1, o2;
}
In the above class Test, data1 and data2 are data
members and function1() and function2() are
member functions.
You can access the data members and member
functions by using a . (dot) operator.
For example,
o2.function1();
This will call the function1() function inside the
Test class for objects o2.
Similarly, the data member can be accessed as:
o1.data2 = 7.7;
It is important to note that, the private members
can be accessed only from inside the class.
So, you can use
o2.function1();
from any function or class in the above example.
However, the code o1.data2 = 7.7; should always
be inside the class Test.
Example 1.
Write a C++ program, where two data
members data1 and data2 and two member
functions insertIntegerData() and insertFloatDa
ta() are defined under Test class.
1.
#include <iostream>
using namespace std;
class Test
{
private:
int data1;
float data2;
public:
void insertIntegerData(int d)
{
data1 = d;
cout << "Number: " << data1;
}
float insertFloatData()
{
cout << "\nEnter data: ";
cin >> data2;
return data2;
}
};
2.
int main()
{
Test o1, o2;
float secondDataOfObject2;
o1.insertIntegerData(12);
secondDataOfObject2 = o2.insertFloatData();
cout << "You entered " <<
secondDataOfObject2;
return 0;
}
Run the program and you will see on the screen
of monitor:
Number: 12
Enter data: 22.27
You entered 22.27
1.Узнал ли я новое и полезное за этот курс?
2. Что понравилось в курсе?
3. Что не понравилось?
4. Что бы Вы хотели изменить в процессе
преподавания?
5. Любые Ваши предложения: