Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
CSE 303 Lecture 16 Multi-file (larger) programs
C++ Classes & Data Abstraction
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Road Map Introduction to object oriented programming. Classes
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
C++ fundamentals.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Object-Oriented Programming in C++
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CS Introduction to Data Structures Spring Term 2004 Franz Hiergeist.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Classes, Interfaces and Packages
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Inheritance & Classification Hierarchies Lecture-8.
Chapter 12 Classes and Abstraction
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Andy Wang Object Oriented Programming in C++ COP 3330
Classes & Objects.
Introduction to Classes
Chapter 5 Classes.
Lecture 4-7 Classes and Objects
Introduction to Classes
Classes and Data Abstraction
Classes and Objects.
Classes C++ representation of an object
Data Structures and ADTs
Lecture 8 Object Oriented Programming (OOP)
SPL – PS1 Introduction to C++.
Presentation transcript:

Abstract Data Types Using Classes Lecture-5

Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords  class keyword to define a class (ADT) ‏  private keyword to define 'hidden' parts of the class  public keyword to define the visible interface of the class  The scope resolution operator (::) used to link together classes and methods

The Anatomy of a class A Class has two parts (just like an ADT) ‏  A private (hidden) part  A public interface The public part defines the behavior of the object (methods) ‏ The private part contains the data It is normal practice to put attributes in the private part of the class where they can only be accessed by methods

The Anatomy of a class It is possible to put attributes (data) in the public part of the class but this undermines encapsulation. It is also possible to put methods in the private part of a class but these may not be used as an external interface (however they may be used by other methods) ‏

The Unified Modelling Language method of drawing class diagrams The first element of the class diagram is the Class name this is the unique name of the Object for the class. Class Name Attributes: Methods()‏

Attributes When drawing attributes the following syntax is used. The scope section of the attribute syntax is used to indicate the visibility of the attribute. [scope] [attribute name] : [data type] scope symbol meaning Private +Public #Protected (no symbol)Implementation

Attributes When drawing attributes the following syntax is used. The attribute name is a unique name for the attribute, and finally the data type represents what type is used for the attribute. For example the following attribute definition represents a private integer called AccountNumber. [scope] [attribute name] : [data type] - AccountNumber : int

Methods When drawing methods the following syntax is used. As with attributes the scope symbols are used to indicate the visibility Next comes the method name. Within the ( ) of the method an optional list of parameters to the method may be placed. These are comma separated and use the following layout. Finally the return type of the method (if any) is written. scope] [Method name]([opt  parameters],): [return type] (ParameterName :int, Parameter2 : char)

Methods For the following example a method called setAccountNum is prototyped, it is passed one integer value an returns a boolean argument. + setAccountNum(AccNumIn :int) : bool  Finally the return type of the method (if any) is written.

Single File Instruction header files define interfaces for functions, structures, unions and classes They may also define variables, however if this header file is then included in more than one module the linker will complain as the same variable is defined twice. To overcome this problem we can use Single File Inclusion The traditional way of doing this is as follows  Use the name of the Header file as an identifier for the pre-processor and create a unique #define name  place a #include directive in the code to define the module name  If the #define does not exist then it will be included if it does there is no need to include it again.

A Class Example #ifndef __ExampleClass_H_ #define __ExampleClass_H_ class ExampleClass { private : int i; public: void SetValue(int value_in)‏ { i=value_in } int GetValue()‏ { return i; } }; //end of class #endif

Some observations on the class A class definition is terminated with a semicolon (unlike a function definition) ‏ The members of a class are private by default, so the private keyword could be omitted. The integer variable 'i' is private and therefore inaccessible from outside the class, except indirectly via the setValue and getValue methods Methods are usually know as 'member functions' in C++. The member functions 'setValue' and 'getValue' are defined 'inline' (inside the class definition). setValue is of void type because it doesn't return a value getValue returns and int (the value of i) ‏

More on declaring Methods In the previous example the methods are declared 'inline' i.e. as part of the class This is a bad idea as the code is duplicated for every object created which  takes up memory  and leads to inefficient coding Most modern compilers will only allow 'inline' functions of about two lines of code or less So What do we do to overcome this limitation ?

The Scope Resolution Operator (::)‏ #ifndef __ExampleClass_H_ #define __ExampleClass_H_ class ExampleClass { private : int i; public: void SetValue(int v_in)‏ int GetValue()‏ }; //end of class #endif #include “ExampleClass.h” void ExampleClass::SetValue(int v_in)‏ { i=v_in } int ExampleClass::GetValue()‏ { return i; } ExampleClass.h ExampleClass.cpp

More on Scope Resolution In the above example the methods are declared as part of the class  however there is no code defined in the class The methods are then prototyped after the class definition using the following return_type Class_name::method_name(parameterlist) ‏ This has the effect that only one version of the method exists at run-time  This method is available to all instances of objects of this class  Thus saving memory and improving efficiency

A more Complex Example The previous example was a bit simple, so here is a more complex one A 2D Point object has two attributes: x and y ( the x value of the point the y value of the point) ‏ And the following methods get x value get y value Modifier Methods set x value set y value move left move right move up move down

Complex Example: Class Diagram We can now generate a class diagram Point2D - x : float x : float + SetX(Xin : float)‏ + SetY(Yin : float)‏ + GetX() : float + GetY() : float + Left() + Right()‏ + Up()‏ + Down()‏

Code #ifndef __POINT2D_H__ #define __POINT2D_H__ class Point2D { private : float x; float y; public : void SetX(float Xin); void SetY(float Yin); float GetX(void); float GetY(void); void Up(void); void Down(void); void Left(void); void Right(void); }; #endif Point2D.h

Code #include”Pont2D.h” void Point2D::SetX(float Xin)‏ {x = Xin; } void Point2D::SetY(float Yin)‏ {y = Yin } float Point2D::GetX(void)‏ { return x; } float Point2D::GetY(void)‏ { return y;} void Point2D::Up(void) {y = y+1.0;} void Point2D::Down(void)‏ {y = y-1.0;} void Point2D::Left(void)‏ {x = x+1.0; } void Point2D::Right(void)‏ {x = x-1.0;} Point2D.cpp

#include using namespace std; #include "Point2D.h" int main(void)‏ { Point2D Pt; point.SetX(1.0); point.SetY(2.0); cout <<"Point value is ["<<Pt.GetX()<<","<<Pt.GetY()<<"]"<<endl; point.Up(); point.Left(); cout <<"Point value is ["<<Pt.GetX()<<","<<Pt.GetY()<<"]"<<endl; point.Right(); point.Down(); cout <<"Point value is ["<<Pt.GetX()<<","<<Pt.GetY()<<"]"<<endl; return 0; } Point2DTest.cpp

Makefiles We are using different files, we need to use makefile as follows OBJECTS=Point2D.o PointTest.o PointTest : $(OBJECTS)‏ g++ -Wall -g $(OBJECTS) -o PointTest Point2D.o : Point2D.cpp Point2D.h g++ -Wall -g -c Point2D.cpp PointTest.o : PointTest.cpp Point2D.h g++ -Wall -g -c PointTest.cpp