Docsity

User-Defined Data Types: Structs and Classes in C++, Slides of Algorithms and Programming

Partial preview of the text

Download User-Defined Data Types: Structs and Classes in C++ and more Slides Algorithms and Programming in PDF only on Docsity!

More About Data

Structures and ADT

C++ Data Types

C++ types an be classified as:

  • Fundamental (or simple or scalar): A data object of one of these types is a single object. - int, double, char, bool, complex, and the related types (unsigned, short, etc.)  primitive data types - enumerations  User-defined Data Types
  • Structured : Collections of data - arrays, structs, unions, classes, valarrays, bitsets, the containers and adapters in STL

Differences

  1. C does not provide classes; C++ provides both structs and classes
  2. Members of a struct by default are public ( can be accessed outside the struct by using the dot operator.) In C++ they can be declared to be private (cannot be accessed outside the struct.)
  3. Members of a class by default are private (cannot be accessed outside the class) but can be explicitly declared to be public.

Variables of a struct data type are

OBJECTS

  • For example,
    • struct temperature { int magnitude; char scale; void display(); void convertC2F(); void convertF2C(); };
  • Create an instance of this user-defined data type (or OBJECT): - temperature t1;
  • Access data and function members of our new object:
    • cout << t1.display();

User-defined data type!

OBJECT of that user-defined data type

C Structs vs. C++ Classes (& Structs)

("traditional" vs "OOP")

C++'s structs and classes model objects that have: ♦ Attributes (characteristics) represented as and

Attributes

Data Members

Operations

Function Members

This leads to a whole new style of programming: object-oriented. Objects are self-contained , possessing their own operations — commonly called the I can do it myself principle — rather than being passed as a parameter to an external function that operates on them and sends them back.  This is called ENCAPSULATION (a Language Construct).

♦ Operations (behaviors) represented as (also called methods ).

data members

function members

C structs

ADT vs. Classes/structs

  • A class/struct is an Implemented Data

Type (a user-defined data type ).

ADT
  • Name
  • Collection of Data Items
  • Operations on the data items

Class/Struct

  • Name
    • E.g., temperature
  • Data Members (also called fields, attributes, and characteristics)
    • E.g., int magnitude and char scale
  • Function Members (also called methods, operations, and behaviours)
    • E.g., display(), etc.Docsity.com

Overview

  • When pre-defined types aren’t enough, create a new data type to represent the object
  • Store the new data type in a class library:
    • Put the class declaration in a header file (.h)  temperature.h
    • Put the definitions of nontrivial class operations in an implementation file (.cpp)  temperature.cpp
    • Compile the implementation file separately from program files (also .cpp)  project.cpp
  • In order for a program to use this new data type:
    • #include the class declaration in the program file
    • Compile the program file (.cpp) separately
    • Link the program’s object file and the library’s object file

e.g., Lab 3

Declaring a Class

Common Forms:

class ClassName { // private by default Declarations of private members public: Declarations of public members };

class ClassName { public: //"interface" of class given first Declarations of public members private: Declarations of private members };

Declaration is in a .h file Definition/ Implementation is in a .cpp file

Information Hiding: a Design

Principle

  • HIDE the data from external access (and

modification!) in order to protect the integrity

of the state of the object

HW
OS

Private Functions

Public Functions (Interface) like t1.display()

Data

Object

Encapsulation vs. Information

Hiding

  • Encapsulation is a language construct
  • Information Hiding is a design principle
  • Related but one is required the other is

recommended

Data

Object

Operations

Header file for class Time — Version

/** Time.h --------------------------------------------------------^1

This header file defines the data type Time for processing time. Basic operations are: Set: To set the time Display: To display the time ----------------------------------------------------------------------/ #include using namespace std; class Time { /******** Member functions ********/ public: / Set sets the data members of a Time object to specified values.

  • Receive: hours, the number of hours in standard time
  • minutes, the number of minutes in standard time
  • AMPM ('A' if AM, 'P' if PM)
  • Postcondition: The Time object containing this function has its
  • myHours, myMinutes, and myAMorPM members set to hours,
  • minutes, and am_pm, respectively, and myMilTime to
  • the equivalent military time ********************************************************************/ void Set(unsigned hours, unsigned minutes, char am_pm);

Note the doc!

Plus: Name, Course #, Lab/Project #, etc.

/* Display displays time in standard and military format using

  • output stream out.
  • Receive: ostream out
  • Output: The time represented by the Time object containing
  • this function
  • Passes back: The ostream out with time inserted into it *****************************************************************/

Notes:

  1. "my" in data members names is a reminder of internal ("I can do it myself") perspective.
  2. const at end of Display()'s prototype makes it a const function which means it cannot modify any of the data members. Good idea to protect data members from accidental modification.

void Display(ostream & out) const; /********** Data Members **********/ private: unsigned myHours, myMinutes; char myAMorPM; // 'A' or 'P' unsigned myMilTime; // military time equivalent }; // end of class declaration

"Accessors" vs. "Mutators"

Implementation of a Class

Class declaration contains:

  • Declarations of data members
  • Prototypes (declarations) of function members

Definitions of function members are not usually placed in class declaration → Avoid cluttering up the interface

Definitions placed outside the class declaration must tell compiler where the corresponding declaration/prototype is :

Use the scope operator :: which has the form ClassName :: ItemName (the qualified or full name of ItemName .)

This applies also to constants and types declared within a class. (^) e.g., Lab 4

class Something { public: static const int CAPACITY = 100; typedef double ArrayType[CAPACITY]; void Print(ArrayType a, int itsSize);

... };

Example : See slide 19

... Something::ArrayType x = {0};

for (int i = 0; i < Something::CAPACITY; i++)

...

void Something::Print(Something::ArrayType a, int itsSize) {... }

Use of static locals inside functions