#oop_d_its - 4th - c++ oop and class structure

35
C++ OOP :: Class Structure 22/06/2022 1 Hadziq Fabroyir - Informatics ITS

Upload: hadziq-fabroyir

Post on 20-Dec-2014

702 views

Category:

Health & Medicine


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 1

C++ OOP :: Class Structure

Page 2: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 2

C++ Structures: Classes

In C++, the concept of structure has been generalized in an object-oriented sense:

classes are types representing groups of similar instanceseach instance has certain fields that define it (instance variables)instances also have functions that can be applied to them (represented as function fields) -- called methodsthe programmer can limit access to parts of the class (to only those functions that need to know about the internals)

Page 3: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 3

A Motivating Example

You declare a simple structure:struct Robot {

float locX;

float locY;

float facing;

};

Robot r1;

What if you never want locX or locY to be negative ?

Someone unaware of your restriction could set:r1.locX = -5;

and you have a problem !

Page 4: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 4

General Class Definition in C++

class ClassName {

access_modifier1:

field11_definition;

field12_definition;

access_modifer2:

field21_definition;

field22_definition;

};

ClassName is a new type (just as if declared as struct with typedef in C)

Possible access modifiers: publicprivateProtected

Fields can be variables (as in C structures) or functions

Page 5: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 5

A Simple Classclass Robot { public: float getX() { return locX; } float getY() { return locY; } float getFacing() { return facing; } void setFacing(float f) { facing = f; } void setLocation(float x, float y); private: float locX; float locY; float facing;};

Page 6: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 6

private Access Modifier

Fields marked as private can only be accessed by functions that are part of that classIn the Robot class, locX, locY, and facing are private float fields, these fields can only be accessed by functions that are in class Robot (getX, getY, getFacing, setFacing, setLocation)Example:void useRobot() {

Robot r1;

r1.locX = -5; // Error

Page 7: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 7

public Access Modifier

Fields marked as public can be accessed by anyoneIn the Robot class, the methods getX, getY, etc. are public

these functions can be called by anyone

Example:void useRobot() { Robot r1; r1.setLocation(-5,-5); // Legal to call

Page 8: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 8

struct VS class

In C++ struct and class can be used interchangeably to create a class with one exceptionWhat if we forget to put an access modifier before the first field?struct Robot { OR class Robot { float locX; float locX;In a class, until an access modifer is supplied,

the fields are assumed to be privateIn a struct, the fields are assumed to be public

Page 9: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 9

What about protected?

C++ allows users to create classes based on other classes:

a FordCar class based on a general Car class

idea: the general Car class has fields (variables and functions that describe all cars)

the FordCar class then uses the fields from the general Car class and adds fields specific to FordCars

done with inheritance

public fields are inherited (as public)

private fields are not inherited

protected fields are like private, but can be inherited

Page 10: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 10

Class MethodsFunctions associated with a class are declared in one of two ways:

ReturnType FuncName(params) { code }

function is both declared and defined (code provided)

ReturnType FuncName(params);

function is merely declared, we must still define the body of the function separately

To call a method we use the . form:

classinstance.FuncName(args);

FuncName is a field just like any other field in the structured variable classinstance

Page 11: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 11

Defined Methodsclass Robot { public: float getX() { return locX; }};Robot r1;

The function getX is defined as part of class Robot

To call this method:

cout << r1.getX() << endl; // prints r1’s locX

Page 12: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 12

Defining Methods Separately

For methods that are declared but not defined in the class we need to provide a separate definition

To define the method, you define it as any other function, except that the name of the function is ClassName::FuncName:: is the scope resolution operator, it

allows us to refer to parts of a class or structure

Page 13: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 13

A Simple Classclass Robot { public: void setLocation(float x, float y); private: float locX; float locY; float facing;};

void Robot::setLocation(float x, float y) { if ((x < 0.0) || (y < 0.0)) cout << “Illegal location!!” << endl; else { locX = x; locY = y; }}

Page 14: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 14

Types of Class Methods

Generally we group class methods into three broad categories:accessors - allow us to access the fields of a class

instance (examples: getX, getY, getFacing), accessors do not change the fields of a class instance

mutators - allow us to change the fields of a class instance (examples: setLocation, setFacing), mutators do change the fields of a class instance

manager functions - specific functions (constructors, destructors) that deal with initializing and destroying class instances (more in a bit)

Page 15: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 15

Accessors & Mutators

Why do we bother with accessors and mutators?

Why provide getX()?? Why not just make the locX field publicly available?

By restricting access using accessors and mutators we make it possible to change the underlying details regarding a class without changing how people who use that class interact with the class

Example: what if I changed the robot representation so that we no longer store locX and locY, instead we use polar coordinates (distance and angle to location)?

If locX is a publicly available field that people have been accessing it, removing it will affect their code

If users interact using only accessors and mutators then we can change things without affecting their code

Page 16: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 16

Rewritten Robot Class

class Robot { public: float getX() { return dist * cos(ang); } float getY() { return dist * sin(ang); } void setLocation(float x, float y); private: float dist; float ang;};void Robot::setLocation(float x, float y) { if ((x < 0.0) || (y < 0.0)) cout << “Illegal location!!” << endl; else { dist = sqrt(x * x + y * y); ang = atan2(y,x); }}

Page 17: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 17

Object-Oriented Idea

Declare implementation-specific fields of class to be private

Provide public accessor and mutator methods that allow other users to connect to the fields of the class

often provide an accessor and mutator for each instance variable that lets you get the current value of that variable and set the current value of that variable

to change implementation, make sure accessors and mutators still provide users what they expect

Page 18: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 18

Static Instance Variables

C++ classes may also contain, static instance fields -- a single field shared by all members of a class

Often used when declaring class constants (since you generally only need one copy of a constant)

To make a field static, add the static keyword in front of the field

can refer to the field like any other field (but remember, everybody shares one copy)

static variables are also considered to be global, you can refer to them without an instance

static fields can be initialized (unlike other fields)

Page 19: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 19

Static Fields Example

class Robot { public: static int numRobots = 0; static const float minX = 0.0; static const float maxX = 100.0; void initializeRobot() { numRobots++; } void setLocation(float x, float y);}void Robot::setLocation(float x, float y) { if ((x < minX) || (x > maxX)) …}

Page 20: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 20

Static Fields as Global Variables

One can examine public static fields of a class outside of that class using the form:ClassName::StaticFieldName

Example:void main() {

Robot r1, r2;

r1.initializeRobot();

r2.initializeRobot();

cout << Robot::numRobots << “ robots.\n”;

Page 21: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 21

The Need for Manager Functions

To make the previous program work we have to explicitly call the routine initializeRobot, wouldn’t it be nice to have a routine that is automatically used to initialize an instance -- C++ doesC++ provides for the definition of manager functions to deal with certain situations:constructors (ctor) - used to set up new instances

default - called for a basic instancecopy - called when a copy of an instance is made (assignment)other - for other construction situations

destructors (dtor) - used when an instance is removed

Page 22: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 22

When Managers Called

void main() { Robot r1; // default ctor (on r1) Robot r2; // default ctor (on r2) Robot* r3ptr;

r3ptr = new Robot; // default ctor on // Robot object r3ptr points to r2 = r1; // copy ctor delete r3ptr; // dtor} // dtor (on r1 and r2) when main ends

Page 23: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 23

The Default Constructor (ctor)

A default constructor has no arguments, it is called when a new instance is created

C++ provides a default ctor that initializes all fields to 0

To write your own, add following to your class:class MyClass {

public:

MyClass() { // repeat class name, no

code here // return type

}

}

Page 24: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 24

Example Default ctor

class Robot {

public:

static int numRobots = 0;

Robot() {

numRobots++;

locX = 0.0;

locY = 0.0;

facing = 3.1415 / 2;

}

private:

float locX;

float locY;

float facing;

}

Page 25: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 25

Destructor (dtor)A destructor is normally not critical, but if your class allocates space on the heap, it is useful to deallocate that space before the object is destroyed

C++ provides a default dtor that does nothing

can only have one dtor

To write your own, add following to your class:class MyClass {

public:

~MyClass() {

code here

}

}

Page 26: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 26

Example dtorclass Robot {

public:

char *robotName;

Robot() {

robotName = 0;

}

void setRobotName(char *name) {

robotName = new char[strlen(name)+1];

strcpy(robotName,name);

}

~Robot() {

delete [] robotName;

}

}

Page 27: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 27

The Copy ctorA copy constructor is used when we need a special method for making a copy of an instance

example, if one instance has a pointer to heap-allocated space, important to allocate its own copy (otherwise, both point to the same thing)

To write your own, add following to your class:class MyClass {

public:

MyClass(const MyClass& obj) {

code here

}

}

Page 28: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 28

Example Copy ctor

class Robot {

public:

char *robotName;

void setRobotName(char *name) {

robotName = new char[strlen(name)+1];

strcpy(robotName,name);

}

Robot(const Robot& obj) {

robotName = new char[strlen(obj.robotName)+1];

strcpy(robotName,obj.robotName);

}

}

Page 29: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 29

Other ctors

It is often useful to provide constructors that allow the user to provide arguments in order to initialize argumentsForm is similar to the copy ctor, except parameters are chosen by programmer:class MyClass {

public;

MyClass(parameters) {

code here

}

}

Page 30: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 30

Example ctor

class Robot {

public:

Robot(float x, float y, float face) {

locX = x;

locY = y;

facing = face;

}

}

calling: Robot r1(5.0,5.0,1.5);

Robot r2(5.0,10.0,0.0);

Robot* rptr;

rptr = new Robot(10.0,5.0,-1.5);

Page 31: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 31

A Combination ctor

Can combine a ctor that requires arguments with the default ctor using default values:

class Robot {

public:

Robot(float x = 0.0, float y = 0.0,

float face = 1.57075) {

locX = x; locY = y; facing = face;

}

}

calling: Robot r1; // ctor called with default args

Robot r2(); // ctor called with default args

Robot r3(5.0); // ctor called with x = 5.0

Robot r4(5.0,5.0); // ctor called with x,y = 5.0

Page 32: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 32

Hiding the Default ctor

Sometimes we want to make sure that the user gives initial values for some fields, and we don’t want them to use the default ctorTo accomplish this we declare an appropriate ctor to be used in creating instances of the class in the public area, then we put the default ctor in the private area (where it cannot be called)

Page 33: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 33

Example ctor

class Robot {

public:

Robot(float x, float y, float face) {

locX = x;

locY = y;

facing = face;

}

private:

Robot() {}

}

calling: Robot r1(5.0,5.0,1.5);

Robot r2; // ERROR, attempts to call default ctor

Page 34: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS 34

For your practice …

Exercise 9.5

Complex ClassProperties:

float realPart, float imaginaryPart Method (Mutators):

Complex addBy(Complex), ComplexSubstractBy(Complex)

Lab Session for upper order (Senin, 15.00-17.00)Lab Session for lower order (Senin, 19.00-21.00)

Please provide:the softcopy (send it by email – deadline Sunday 23:59)the hardcopy (bring it when attending the lab session)

Page 35: #OOP_D_ITS - 4th - C++ Oop And Class Structure

10/04/2023Hadziq Fabroyir - Informatics ITS

☺~ Next: C++ OOP :: Overloading ~☺

[ 35 ]