defining data types in c++
DESCRIPTION
Defining Data Types in C++. Part 2: classes. Quick review of OOP. Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object Class definition includes: member functions member variables. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/1.jpg)
Defining Data Types in C++
Part 2: classes
![Page 2: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/2.jpg)
Quick review of OOP
• Object: combination of:– data structures (describe object attributes)– functions (describe object behaviors)
• Class: C++ mechanism used to represent an object
• Class definition includes:– member functions– member variables
![Page 3: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/3.jpg)
Information Hiding
• Principle says we should only know what we need to know (to prevent getting bogged down in irrelevant detail)
• A class that uses information hiding in its design is an Abstract Data Type (ADT– member variables hidden– access to hidden members available only
through member functions
![Page 4: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/4.jpg)
Class declaration promotes information hiding
class Name{public://member functions//declared hereprivate://member variables//declared here
};
• “Name” is a valid identifier
• public section includes class parts accessible to outside world
• private section includes hidden parts
• Note semicolon!
![Page 5: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/5.jpg)
Example object: the Nesper sign
• Member variables:– message– time & temperature
• Member functions:– display message– display time & temp– change message– change time & temp
![Page 6: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/6.jpg)
class Nesper{
public:void display_message ( ) const;void display_tnt ( ) const;void change_message (char[] text);void change_tnt (int temp, clock current);private:char message[25];int temperature;clock time;
};
![Page 7: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/7.jpg)
Class definition: public section
class Nesper{
public:void display_message ( ) const;void display_tnt ( ) const;
...
• Public section contains member function prototypes
• Keyword “const” indicates that the functions declared here do not change the object
![Page 8: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/8.jpg)
Class definition: public section
...void change_message (char[] text);void change_tnt (int temp,
clock current);
• “Change” functions are examples of modifiers -- member functions that alter the calling object
• change_tnt includes a parameter (current) that is an example of an ADT variable
![Page 9: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/9.jpg)
Class definition: private section
…
private:char message[25];int temperature;clock time;
};
• Member variables declared here
• Can be variables of built-in types, arrays or pointer, or instances of objects defined elsewhere
![Page 10: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/10.jpg)
Where do they go?
• Class declaration: header file (xxxx.h)• Member function definitions: definition file
– xxxx.cpp (same name as header -- different extension)
– should not include a main( ) function• Class instances: user program
![Page 11: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/11.jpg)
Implementing member functions
#include <iostream.h>#include <string>#include <assert.h>void Nesper::change_message
(char[] text){
assert (strlen(text)<25);message = text;
}
• Preprocessor directives for all needed library routines
• Class name and scope operator (::) indicate this is a member function
![Page 12: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/12.jpg)
Notes on member functions
• Every instance of a class (object) has its own copies of all class members, including member functions
• A member function is called by the object that owns it (can have several objects of same class in a program)
• Member functions may call other member functions
![Page 13: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/13.jpg)
Using a class in a program
#include “nesper.h”
int main( ){
Nesper sign1, sign2;
...
• Preprocessor directive indicates class definition should be included
• Declaring Nesper variable instantiates the class
• sign1 and sign2 are objects of type Nesper
![Page 14: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/14.jpg)
Using a class in a program
…sign1.change_message(“This is my sign”);sign1.display_message( );
...
• Member function is activated by calling object
• Syntax:calling_object.function_name(argument(s));
![Page 15: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/15.jpg)
Constructors
• Special member function• Automatically called when an object is
instantiated• Unique characteristics:
– Constructor name is same as class name– Constructor has no return value (not even void)
• Can have multiple constructors for a class -- example of function overloading
![Page 16: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/16.jpg)
Why define constructors?
• Can define a class without one; in this case, compiler uses automatic default constructor– memory allocated for member variables– constructors for individual members are called,
if they exist• With defined constructor, can do more --
including initializing member variables
![Page 17: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/17.jpg)
Constructor Prototypes
class Nesper{
public:Nesper (string msg); // initializes messageNesper (int temp); // initializes temperatureNesper (clock tm); // initializes timeNesper ( ); // default constructor
![Page 18: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/18.jpg)
Notes on Function Overloading
• Can have as many functions with the same name in a class as you wish
• Compiler differentiates between the functions by their parameter lists
• A constructor that requires no arguments is the default constructor -- will be called under most circumstances
![Page 19: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/19.jpg)
Calling a constructor
• Constructor is called when an object is declared:Nesper stopsign (“Stop! Stop, I say!”);
// calls first ctor in class
Nesper midnight (12:00);// calls ctor with clock parameter
Nesper sign; // calls default constructor
![Page 20: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/20.jpg)
Constructor implementation
Nesper::Nesper (string msg){
message = msg;}
![Page 21: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/21.jpg)
Another example: default ctor
Nesper::Nesper( ){
message = “Your message here”;temperature = 32;time = 12:00;
}
![Page 22: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/22.jpg)
Default arguments
• Functions can be declared with default values listed for their parameters
• Provides flexibility for constructors:– can be called with or without arguments– can be called with some, but not all arguments
specified– if no arguments are specified, default values are
used
![Page 23: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/23.jpg)
Nesper constructor with default arguments
class Nesper{
public:Nesper (string msg =
“Your message here”, int temp = 32,
clock tm = 12:00);...
• Default arguments are specified in the function prototype, not the implementation
• When function is called, can omit some or all arguments -- may be omitted starting from right
![Page 24: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/24.jpg)
Examples of function calls
Nesper mysign (“Taurus”, 65, 4:30);// all defaults replaced by actual arguments
Nesper yoursign (“Pisces”);// msg replaced by argument; use defaults for// remaining values
Nesper sign;// most common -- defaults used for all values
![Page 25: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/25.jpg)
Implementation of function with default arguments
Nesper::Nesper (string msg, int temp, clock tm)
{message = msg;temperature = temp;time = tm;
}
• Implementation is identical to version that called for the same parameters but didn’t use default arguments
• Default arguments appear only in prototype
![Page 26: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/26.jpg)
One more variation: inline functions
• Inline functions are defined (implemented) within the class definition– Saves a little execution time (no function call,
no return)– Can be inefficient in terms of memory (can end
up with many copies of same compiled code• Best for extremely simple, “one-liner”
functions
![Page 27: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/27.jpg)
Inline constructor exampleclass Nesper{
public:Nesper( ){message = “Your message”;temperature = 32;time = 12:00;} ...
• Inline functions aren’t usually used for constructors, unless the object is very small
• This is still just one function declaration within class definition
![Page 28: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/28.jpg)
Value semantics
• Operations that determine how values are copied from one object to another object of the same class type
• Assignment operator• Copy constructor: constructor that
instantiates an object which is an exact copy of its argument
![Page 29: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/29.jpg)
Automatic assignment example
Nesper sign1(“Hi”, 32, 1:00), sign2;
// sign2 has default values
sign2 = sign1;// sign2 now has same// values as sign1
• Can use automatic assignment when the object doesn’t use dynamic memory
• Later we’ll see how to define the assignment operation for classes that require it
![Page 30: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/30.jpg)
Automatic copy constructor
Nesper sign1;…Nesper sign2(sign1);...Nesper sign3 = sign1;
• First object uses default constructor
• Both second and third objects use copy constructor, even though third example looks like assignment
• Like automatic assignment, must be explicitly defined for some classes
![Page 31: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/31.jpg)
Assignment vs. copy constructor
• Assignment copies information from one existing object into another existing object
• Copy constructor declares and initializes a new object, which is a copy of an existing object
![Page 32: Defining Data Types in C++](https://reader033.vdocuments.site/reader033/viewer/2022051518/56816812550346895ddda091/html5/thumbnails/32.jpg)
One more look at Nesper.h#ifndef NESPER_H // macro guard -- use to ensure that class declaration#define NESPER_H // only appears once in a program -- safeguard
// to prevent linking errors in large programsclass Nesper{
public:Nesper (string msg=“Your message”, int temp=32, clock tm=12:00);void display_message( ) const;void display_tnt( ) const;void change_message (string);void change_tnt (int temp, clock tm);private:string message;int temperature;clock time;
};#endif