user-defined classes

27
User-Defined Classes

Upload: maddy

Post on 04-Feb-2016

55 views

Category:

Documents


0 download

DESCRIPTION

User-Defined Classes. Class Definition. int, float, char are built into C++ Declare and use int x = 5; Create user defined data types Extensive use throughout remainder of course Counter class will introduce the concept counter.h counter class definitions Define the class in the .h file. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: User-Defined Classes

User-Defined Classes

Page 2: User-Defined Classes

2

Class Definition

int, float, char are built into C++ Declare and use

– int x = 5; Create user defined data types

– Extensive use throughout remainder of course Counter class will introduce the concept

– counter.h counter class definitions– Define the class in the .h file

Page 3: User-Defined Classes

3

User Defined Types

Define what data we want in the class– data elements of the class are private

Create the functionality to operate on the data– class member functions access data

We have created a new data type not known to the compiler

Page 4: User-Defined Classes

4

Comparing struct and classes

struct & class define a data type– Collection of related elements– Prototype declarations– Three levels of access control– Public– Private– Protected

Major difference default access opposite– class private - struct public

Page 5: User-Defined Classes

5

Syntax

Form: class class_name

{

public:

List all public data and functions

private:

List all private data and functions

};

Page 6: User-Defined Classes

6

Counter.h

// FILE: Counter.h

// COUNTER CLASS DEFINITION

class counter

{

public:

counter ();

counter (int m_v);

Page 7: User-Defined Classes

7

Counter.h

// SET COUNTER VALUE

void setcount (int val);

// INCREMENT COUNTER

void increment ();

// DECREMENT COUNTER

void decrement ();

// RETURN CURRENT COUNTER VALUE

int getCount ();

Page 8: User-Defined Classes

8

Counter.h

// RETURN MAXIMUM COUNTER VALUE

int get_maxvalue ();

private:

// Data Members (Attributes)...

int count;

int max_value;

};

Page 9: User-Defined Classes

9

Class Implementation

Counter.cpp implementation details Hidden from users (details) Scope resolution operator

– :: prefix for each member function

– Informs the compiler that the function is a member of the class

Class member functions can access all class data members– Avoid using member functions inside member functions

Page 10: User-Defined Classes

10

Constructors

Two special member functions– Same name as class name

Constructor executes each time an object of type counter is declared– counter mike;

Initializes object Types

– Default– Class

Page 11: User-Defined Classes

11

Constructors

Default Constructor – Used when no arguments passed as part of the

declaration Class Constructor

– Used when arguments are passed as part of the declaration

Constructors do NOT specify a return type

Page 12: User-Defined Classes

12

Member Functions

Member functions that modify data are– set_value– increment– decrement

Member functions that retrieve data are– get_value– get_maxvalue

Page 13: User-Defined Classes

13

Counter.cpp

// FILE: Counter.cpp

// COUNTER CLASS DEFINITION

#include "Counter.h"

#include <iostream.h>

#include <limits.h>

// DEFAULT CONSTRUCTOR

counter :: counter ()

{

value = 0;

max_value = INT_MAX;

}

Page 14: User-Defined Classes

14

Counter.cpp

// CONSTRUCTOR WITH ARGUMENT

counter :: counter (int m_v)

{

count = 0;

max_value = m_v;

}

Page 15: User-Defined Classes

15

Counter.cpp

// SET COUNTER VALUE

void counter::setCount (int val)

{

if (val >= 0 && val <= max_value)

count = val;

else

cout << "New value is out of range.

Value not changed." << endl;

}

Page 16: User-Defined Classes

16

Counter.cpp

// INCREMENT COUNTER

void counter::increment ()

{

if (count < max_value)

count++;

else

cout << "Counter overflow. Increment

ignored." << endl;

}

Page 17: User-Defined Classes

17

Counter.cpp

// DECREMENT COUNTER

void counter::decrement ()

{

if (count > 0)

count--;

else

cout << "Counter underflow. Decrement

ignored." << endl;

} // end decrement

Page 18: User-Defined Classes

18

Counter.cpp

// RETURN CURRENT COUNTER VALUE

int counter::getcount ()

{

return count;

} // end getvalue

// RETURN MAXIMUM COUNTER VALUE

int counter::get_maxvalue ()

{

return max_value;

} // end get_maxvalue

Page 19: User-Defined Classes

19

Using the counter Class

Driver program CntrTest.cpp will help us see how the Counter Class is used

Must #include “Counter.cpp” because our class definitions are contained in it.

Page 20: User-Defined Classes

20

Use of Classes and Objects

Class Instance– counter c1;– Creates an instance of the counter class (object)– Default constructor invoked– Allocates space in memory for object

Similar to other data type declarations– int value;– Space in memory allocated for a data type int

Page 21: User-Defined Classes

21

Private vs Public

Public member functions allow users to operate on the counter object c_1

May have private member functions– If member functions need to call another function

not part of the class it should be private Use care when defining public access Users of a class are clients Class sometimes referred to as the server

Page 22: User-Defined Classes

22

CounterTest.cpp

// FILE: CounterTest.cpp

// TEST PROGRAM FOR Counter CLASS

#include <iostream.h>

#include "Counter.cpp"

int main ()

{

// Local data ...

counter c1;

counter c2 (10);

Page 23: User-Defined Classes

23

CounterTest.cpp

c1.set_value (50);

c1.decrement ();

c1.decrement ();

c1.increment ();

cout << "Final value of c_1 is " <<

c1.getCount () << endl;

c2.increment ();

c2.increment ();

c2.decrement ();

cout << "Final value of c_2 is " <<

c2.getCount () << endl;

return 0;

}

Page 24: User-Defined Classes

24

Function Overloading & Polymorphism

Functions with the same name is called function overloading

Polymorphism is what allows functions with the same name to do different things based on its arguments

Page 25: User-Defined Classes

25

Destructors

Member function automatically called when an object is destroyed

Destructor name is ~classname, e.g., ~Square Has no return type; takes no arguments Only 1 destructor per class, i.e., it cannot be

overloaded If constructor allocates dynamic memory,

destructor will release memory

Page 26: User-Defined Classes

26

Arrays of Objects

Objects can be the elements of an array:Square lottaSquares[10];

Default constructor for object is used when array is defined

Must use initializer list to invoke constructor that takes arguments:Square triSqu[3] = {5,7,11};

More complex initialization if constructor takes > 1 argument

Page 27: User-Defined Classes

27

Accessing Array Objects

Objects in an array are referenced using subscripts

Member functions are referenced using dot notation:lottaSquares[3].setSide(6);

cout << triSqu[i].getSide;