Top Banner
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private Public function members are part of class interface Private class members are helpers, can be accessed only by the class member functions (and friend functions).
22

1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

Jan 18, 2018

Download

Documents

Joel Bond

3 What is an object? OBJECT Operations Data set of methods (member functions) internal state (values of private data members)
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

1

Review:C++ class

• 2 kinds of class members: data members and function members

• Class members are private by default

• Data members are generally private

• Public function members are part of class interface

• Private class members are helpers, can be accessed only by the class member functions (and friend functions).

Page 2: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

2

ObjectsObjects: variables or instances of a class

that is declared in a programDeclaration of an Object Initiation of ObjectsConstructors & DestructorsWorking with Multiple Files

Page 3: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

3

What is an object?

OBJECT

Operations

Data

set of methods(member functions)

internal state(values of private data members)

Page 4: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

4

class Rectangle{

private: int width; int length;public: void set(int w, int l); int area();

}

Declaration of an Object

main(){

Rectangle r1;

r1.set(5, 8); }

r1 is automatically allocated

widthlength

r1width = 5length = 8

Page 5: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

5

class Rectangle{

private: int width; int length;public: void set(int w, int l); int area();

}

Declaration of an Object

main(){ Rectangle r1;

r1.set(5, 8);

Rectangle *r2; r2 = &r1; r2->set(8,10);}

r2 is a pointer to a Rectangle object

???

r2 60005000

width = 8length = 10

//dot notation

//arrow notation

Page 6: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

6

class Rectangle{

private: int width; int length;public: void set(int w, int l); int area();

}

Declaration of an Object

main(){

Rectangle *r3; r3 = new Rectangle();

r3->set(80,100);

delete r3; r3 = NULL;

}

r3 is dynamically allocated in heap

width = 80length = 100

NULL

//arrow notation

Page 7: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

7

#include <iostream.h>

class circle {

public: double radius;

};

Object Initialization

int main(){ circle c1; // Declare an instance of the class circle c1.radius = 5; // Initialize by assignment

}

1. By Assignment

• Only work for public data members

• No control over the operations on data members

Page 8: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

8

#include <iostream.h>

class circle { private: double radius;

public: void set (double r) {radius = r;} double get_r () {return radius;}

};

int main(void) { circle c; // an object of circle class c.set(5.0); // initialize an object with a public member function cout << "The radius of circle c is " << c.get_r() << endl;

// access a private data member with an accessor}

Object Initialization

2. By Public Member Functions

• Accessor

• Implementor

Page 9: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

9

class Rectangle{

private: int width; int length;public: void set(int w, int l); int area();

}

Declaration of an Object

main(){ Rectangle r1;

r1.set(5, 8);

Rectangle *r2; r2 = &r1; r2->set(8,10);}

r2 is a pointer to a Rectangle object

//dot notation

//arrow notation

r1 and r2 are both initialized by public member function set

Page 10: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

10

class Rectangle{

private: int width; int length;public: Rectangle(); Rectangle(const Rectangle &r); Rectangle(int w, int l); void set(int w, int l); int area();

}

Object Initialization3. By Constructor

• Default constructor

• Copy constructor

• Constructor with parameters

Page 11: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

11

class Rectangle{

private: int width; int length;public: void set(int w, int l); int area();

}

Object Initialization

• Default constructor

When a class is declared with no constructors,the compiler automatically assumes default constructor and copy constructor for it.

Rectangle :: Rectangle() { };

• Copy constructor

Rectangle :: Rectangle (const Rectangle & r)

{ width = r.width; length = r.length;

};

Page 12: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

12

class Rectangle{

private: int width; int length;public: void set(int w, int l); int area();

}

Object Initialization

• Initialize with default constructor

Rectangle r1;

Rectangle *r3 = new Rectangle();

• Initialize with copy constructor

Rectangle r4;r4.set(60,80);

Rectangle r5 = Rectangle(r4);

Rectangle *r6 = new Rectangle(r4);

Page 13: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

13

class Rectangle{

private: int width; int length;public: Rectangle(int w, int l){width =w; length=l;} void set(int w, int l); int area();

}

Object Initialization

If any constructor with any number of parameters is declared, no default constructor will exist, unless you define it.

Rectangle r4; // error

• Initialize with constructor

Rectangle r5(60,80);

Rectangle *r6 = new Rectangle(60,80);

Page 14: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

14

class Rectangle{

private: int width; int length;public: Rectangle(); Rectangle(int w, int l); void set(int w, int l); int area();

}

Object InitializationWrite your own constructors

Rectangle :: Rectangle() {

width = 20;length = 50;

};

Rectangle *r7 = new Rectangle();

widthlengthwidth = 20length = 50

5000???

r7 60005000

Page 15: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

15

class Account{

private: char *name; double balance; unsigned int id; //uniquepublic: Account(); Account(const Account &a); Account(const char *person);

}

Object InitializationWith constructors, we have more control over the data members

Account :: Account() {

name = NULL; balance = 0.0;id = get_unique_id();

};

Account :: Account(const Account &a) {

name = new char[strlen(a.name)+1];strcpy (name, a.name);balance = a.balance;id = get_unique_id();

};

Account :: Account(const char *person) {

name = new char[strlen(person)+1];strcpy (name, person);balance = 0.0;id = get_unique_id();

};

Page 16: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

16

So far, …• An object can be initialized by a class

constructor– default constructor– copy constructor– constructor with parameters

• Resources are allocated when an object is initialized

• Resources should be revoked when an object is about to end its lifetime

Page 17: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

Destructors

• Destructors– Special member function– Same name as class

• Preceded with tilde (~)

– No arguments – No return value– Cannot be overloaded– Before system reclaims object’s memory

• Reuse memory for new objects• Mainly used to de-allocate dynamic memory locations

Page 18: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

18

Cleanup of An Object

class Account{

private: char *name; double balance; unsigned int id; //uniquepublic: Account(); Account(const Account &a); Account(const char *person); ~Account();

}

Destructor

Account :: ~Account(){

delete[] name;release_id (id);

}

• Its name is the class name preceded by a ~ (tilde)

• It has no argument• It is used to release dynamically

allocated memory and to perform other "cleanup" activities

• It is executed automatically when the object goes out of scope

Page 19: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

Another class Example

• This class shows how to handle time parts. class Time

{ private:

int *hour,*minute,*second; public:

Time();Time(int h,int m,int s);void printTime();void setTime(int h,int m,int s);int getHour(){return *hour;}int getMinute(){return *minute;}int getSecond(){return *second;}void setHour(int h){*hour = h;}void setMinute(int m){*minute = m;}void setSecond(int s){*second = s;}~Time();

};

Destructor

Page 20: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

Time::Time(){

hour = new int;minute = new int;second = new int;*hour = *minute = *second = 0;

}

Time::Time(int h,int m,int s){

hour = new int;minute = new int;second = new int;*hour = h;*minute = m;*second = s;

}

void Time::setTime(int h,int m,int s){

*hour = h;*minute = m;*second = s;

}

Dynamic locations should be allocated

to pointers first

Page 21: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

void Time::printTime(){ cout<<"The time is : ("<<*hour<<":"<<*minute<<":"<<*second<<")"

<<endl;}

Time::~Time(){

delete hour; delete minute;delete second;}

void main(){

Time *t;t= new Time(3,55,54);t->printTime();

t->setHour(7);t->setMinute(17);t->setSecond(43);

t->printTime();

delete t;}

Output:The time is : (3:55:54)The time is : (7:17:43)Press any key to continue

Destructor: used here to de-allocate memory locations

When executed, the destructor is called

Page 22: 1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.

Reasons for OOP

1. Simplify programming2. Interfaces

• Information hiding:– Implementation details hidden within classes

themselves

3. Software reuse• Class objects included as members of other

classes