Structures/Classes CS 308 – Data Structures. What is a structure? It is an aggregate data type built using elements of other types. Declaring a structure.

Slides:



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

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Structure.
C++ Classes & Data Abstraction
Starting Out with C++: Early Objects 5th Edition
Constructors & Destructors Review CS 308 – Data Structures.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1. Structures Structures : Aggregate data types built using elements of other types struct Time { int hour;
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
C++ Review CS 302 – Data Structures Review Topics Calling functions by value or reference Pointers and reference variables Static and dynamic arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
1 3/2/05CS250 Introduction to Computer Science II Composition and friend Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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 II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
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.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 12 Classes and Abstraction
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Procedural and Object-Oriented Programming
Access Functions and Friend Functions
Class and Objects UNIT II.
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?
Abstract Data Types Programmer-created data types that specify
CONSTRUCTORS & DESTRUCTORS
Concepts of Constructors and Its Types
Review: Two Programming Paradigms
Introduction to Classes
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?
Chapter 5 Classes.
Constructors & Destructors
This technique is Called “Divide and Conquer”.
Lecture 4-7 Classes and Objects
Introduction to Classes
Classes and Objects.
Submitted By : Veenu Saini Lecturer (IT)
Object Oriented Programming (OOP) Lecture No. 12
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Structures/Classes CS 308 – Data Structures

What is a structure? It is an aggregate data type built using elements of other types. Declaring a structure requires declaring its members and their data types. struct rectangle { float height; float width; int xpos; int ypos; };

Structure variables They are declared like variables of any other type. rectangle rc; rectangle &rcRef = rc; rectangle *rcPtr = &rc;

Accessing members of structures dot operator (.): rc.height = 15.89; rcRef.height = 15.89; arrow operator (->): rcPtr -> height = 15.89; or (*rcPtr).height = 15.89; Important: the parentheses around *rcPtr are necessary since the member operator. takes precedence over the dereference operator.

Member functions (methods) Functions which operate on the data of the structure. The prototype of a member function appears within the structure definition. struct rectangle { float height; float width; int xpos; int ypos; void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

Member function declaration Usually, they are declared outside the structure. data_type structure_name::function_name(arguments); ( :: is the "scope resolution operator") void rectangle::draw() { cout << "position is " << xpos << ypos << endl; } void rectangle::posn(int x, int y) { xpos = x; ypos = y; } void rectangle::move(int dx, int dy) { xpos += dx; ypos += dy; }

Referring to a member function We refer to a member function just as any other variable of the structure. rc.draw(); rc.posn(100, 100); rc.move(50, 50);

Information Hiding

Philosophy behind information hiding The actual data representation used within a structure is of no concern to the structure's clients. Protects data members from receiving invalid values. It promotes program modifiability (if the representation of data changes, only the member functions need to change).

Controlling access to members Most common member access specifiers are: public and private struct rectangle { private: float height; float width; int xpos; int ypos; The private keyword specifies that the structure members following it are private to the structure and can only be accessed by member functions (and by friend functions) public: void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

The public keyword specifies that the structure members following it are public to the structure and may be accessed from outside the structure. void main() { rectangle rc; rc.height = 20; // Error: not accessible } Another way to access private data members is by using get-set member functions which are public to the structure. Controlling access to members (cont.)

Classes

What is a class? Practically, there are no differences between structures and classes: (i) A class is a structure which has all of its members private by default. (ii) Structures have all of their members public by default. class rectangle { private: // not needed but included for clarity float height; float width; int xpos; int ypos; public: void draw(); // draw member function void posn(int, int); // position member function void move(int, int); // move member function };

What is an object? An instance or an object is a variable of type class.

Class-based programming Data and functions co-exist inside a class. Member functions are called without passing the data members of the class to them. There is far less chance of misusing functions by passing them the wrong data.