Week 2 L 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CS1004- Object-Oriented

Programming (OOP)
Week 02
Object v/s Classes
Program as Example

Write Code that take input from user student-id , student-age , student-name and
print all information.
See demo 2
Multiple Objects

You can create multiple objects of one class.


The benefit of multiple instances of a single class is that each of them can hold different
values to their variables, and the methods only affect that specific object, so each of them
have the same skeleton, but are in independent states.
An example of this is an enemy class in a game. If you have multiple enemies, you can
create multiple instances of a class, where each enemy is an instance. They all may be in a
different position, which could be a private variable of the class.
Demo Example
Structure vs classes

In C++, a structure is the same as a class except for a few differences.


The most important of them is security.
A Structure is not secure and cannot hide its implementation details from the end user while a class
is secure and can hide its programming and designing details.
Following are the points that expound on this difference:
What if we forget to put an access modifier before the first field?
Data Members of a class are private by default and members of a struct are public by default.
struct Robot {float locX;
OR
class Robot {float locX;
Transformation From Procedural To Object Oriented
Programming-Example
Transformation From Procedural To Object Oriented
Programming-Example
Access specifiers

There are three access specifiers


‘public’ is used to tell that member can be accessed whenever you have access to the object
‘private’ is used to tell that member can only be accessed from a member function
‘protected’ to be discussed when we cover inheritance
Access specifiers-Example

class BankAccount
{
int accountNo;
private:
int PIN;
public:
int accountType;
}
Getter/Setter Functions

Getter functions (or accessor functions)


are used to read value of a private
member of some class

Setter functions (or mutator functions)


are used to modify the value of a
private member of some class
Getter/Setter Functions
Example:
Accessing members

Members of an object can be accessed using


dot operator (.) to access via the variable name
arrow operator (->) to access via a pointer to an object
Member variables and member functions are accessed in a similar fashion
Accessing members
Example

You might also like