• No results found

Class Basics. Simple Class Example A Date Class. Public Access Example Simple Date Class. Basic Philosophy

N/A
N/A
Protected

Academic year: 2021

Share "Class Basics. Simple Class Example A Date Class. Public Access Example Simple Date Class. Basic Philosophy"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

Class Basics

• Classes have two primary characteristics:

– they hold data – data members

– they have functions for manipulating that data – member functions or methods

• Classes have two major types of data members and methods:

– public - non class members (e.g., main program, members of other classes, etc.) can access directly

– private - non class members cannot access directly, only via public methods

– (another type, protected, is not discussed here)

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 1

Simple Class Example – A Date Class

//file date1.h - class Date holds month, //day and year as ints class Date

{ public:

Date();

//creates default date, month,day,year=0,0,0 Date(int m, int d, int y);

//creates date with month, day, year = m,d,y void set(int m, int d, int y);

//sets month,day, year = m,d,y void get(int& m, int& d, int& y);

// returns month, day,year values by reference void output();

// outputs date to default output private:

int month, // date’s month day, // date’s day year;}; //date’s year

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 2

Basic Philosophy

• Hide data from users

• Hide implementation from users

• Provide user access via public methods – controls data manipulation

– can change implementation without affecting users

Public Access Example – Simple Date Class

void Date::set(int m, int d, inty y) {

month = m;

day = d;

year = y;

}

void Date::get(int& m, int& d, int& y) {

m = month;

d = day;

y = year;

}

(2)

Constructors

• A class member function such that:

– automatically called when object declared – if constructor has arguments, must be

given

– constructor must have same name as class – constructors cannot return values

• Must have a default - no arguments (system or user supplied) but may have many (with different signatures)

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 5

Example of Multiple Constructors – The Date Class

Date::Date() {

month = day = year = 0;

}

Date::Date(int m, int d, int y) {

month = m;

day = d;

year = y;

}

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 6

Some Important public Member Functions

• Default Constructor

– use to provide appropriate default values for data members

– not good idea to use “default” default constructor

• Functions to Provide Access to set/Retrieve Private Data Members as Needed

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 7

const Member Functions

• If function does not modify data, declare const.

• E.G., For Date retrieval, Define:

void get(int& m, int& d, int& y) const;

• This results in:

– a compile error if get tries to modify data – allows this function to be called on an

object passed as a const reference

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 8

(3)

Const member functions (cont)

• Functions can return any meaningful type, including a class

• e.g.,

Date min_date(const Date& D1, const Date& D2){

//creates a Date that is the minimum //-- earlier -- of two

//dates D1,D2 --

//must use dates via const functions

• Note: Also uses copy constructor – more later

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 9

Copy Constructors

To make copies of objects – for instance, for value parameters and when initializing a new object with an existing object.

PDQ(const PDQ & newpdq);

Called when an object of type PDQ is passed as a value parameter, when the return value of a function is an object of type PDQ, and whenever an object is initialized in a declaration. Important not to use the default if class has dynamically allocated memory.

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 10

Destructors

To deallocate dynamically allocated memory within an object.

~PDQ();

The destructor is called whenever an object goes out of scope, or an object is destroyed by delete.

It is important not to use the default destructor if an object contains a pointer to dynamically allocated memory.

Assignment Operator

To assign one object to another.

const PDQ & operator=(const PDQ & rightSide);

{

.... copy rightSide return *this;

}

A default definition of = is always included. But does not work properly if class uses dynamically allocated memory. So, then it is necessary to define an assignment operator for the class.

(4)

Friend Functions

A friend function of a class is defined outside that class’s scope, yet has the right to access non-public members of the class.

Standalone functions or entire classes may be declared to be friends of another class.

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 13

Friend Functions (cont)

To give function setX access to private methods, members of classOne, put the following in the class declaration of classOne:

friend void setx(int);

setX is not a member of any class-standalone function.

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 14

Friend Functions (cont)

to declare all member functions of classTwo as friends of classOne, put the following in the definition of classOne:

friend classTwo;

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 15

Examples of New Usage

• Dynamically Create an Array of 5 Ints int *A_ptr;

A_ptr = new int[5];

• Dynamically Create a Single Double double *p;

p = new double;

• Dynamically Create an Array of 50 Dates Date *D_ptr;

D_ptr = new Date[50];

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 16

(5)

Arrow Operator

Date* DPtr;

DPTR = new Date;

(*DPtr).day = 3; // Ignoring Private Protection

DPtr->day = 3;

DPtr->get(m,d,y);

Program Devel/Intro Alg Lang CS501b/275 Spring 2005 17

References

Related documents

The mission of the Jacksonville University School of Aviation’s Aviation Management and Flight Operations major is to produce professional aviators possessing the leadership

 Private methods of the base class are not accessible to a derived class (unless the derived class is a friend of the base class).  If the subclass is derived

 Private methods of the base class are not accessible to a derived class (unless the derived class is a friend of the base class).  If the subclass is derived

La estancia hospitalaria es relevante, ya que definiendo y (re)definiendo roles, asumiendo mientras más tiempo tenga el cuidador principal la enfermera un rol de

King Amenhotep IV who changed his name to Akhenaten smashed the images of Amun and got his temples closed?. Tut oversaw a restoration of the

Growth response for weight gain and length gain in uremic rats following treatment with rhGH and with rhIGF-I.. Dose

Study 1/Research query 1: What are the supports and barriers to occupation based practice experienced by occupational therapists at a pediatric medical facility.. Study

Conclusions: Fifteen years after the screening programme started, this study supports an important decrease in breast cancer mortality due to the screening programme, with