1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch 1.1 - 1.6 Read Style Guide (see course webpage)

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Abstract Data Types Chapter 1. 2 Objectives You will be able to: 1. Say what an abstract data type is. 2. Implement a simple abstract data type in C++
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
C++ Classes & Data Abstraction
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 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 ADT Specification Example TYPE Student DOMAIN Each student can be represented by first name, last name, id and credit hour. OPERATIONS Set each data.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Defining New Types Lecture 21 Hartmut Kaiser
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
C++ Review (3) Structs, Classes, Data Abstraction.
1 CSC241: Object Oriented Programming Lecture No 06.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Object-Oriented Programming Using C++ CLASS 1. 2 Review of Syllabus Catalog Description –An introduction to object oriented programming techniques using.
1 Chapter Structured Types, Data Abstraction and Classes Dale/Weems.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
CS 210 DATA STRUCTURES AND ALGORITHIMS Fall 2006.
Classes Structured Programming 256 Chapter 8 Classes - Part I OOP & Class Object Terminology File Management.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 5.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
Chapter 12 Classes and Abstraction
Eine By: Avinash Reddy 09/29/2016.
Abstract Data Types Programmer-created data types that specify
Chapter Structured Types, Data Abstraction and Classes
Introduction to Structured Data Types and Classes
Lecture 13 Writing Classes Richard Gesick.
CS148 Introduction to Programming II
Classes and Data Abstraction
Learning Objectives Classes Constructors Principles of OOP
CS410 – Software Engineering Lecture #5: C++ Basics III
CS 144 Advanced C++ Programming February 21 Class Meeting
Data Structures and ADTs
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
CS148 Introduction to Programming II
Classes and Objects Systems Programming.
Abstract Data Types Stacks CSCI 240
Introduction to Classes and Objects
Presentation transcript:

1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)

2 Abstraction and Data Hiding Data Abstraction: Captures essential qualities of an object and names the object. Ignores implementation details Is necessary for managing large, complex software projects. Data Hiding: Hides implementation details from user Prevents user from accessing implementation directly User must interact with object through an interface.

3 Classes in C++ A class is a means of abstraction in C++ A class is a specification of a group of objects that all have the same essential properties FOR EXAMPLE...

4 A class example class Student Properties (data members) name, graduation year, list of courses, number of courses Operations (methods) List Courses List Student Information Add a Course Set Graduation Date

5 An object is an instance of a class For example: object studentA Name: Andrea Student Graduation Year: 2007 List of Courses: CSCI132, MATH331 Number of courses: 2

class Student Specification // SPECIFICATION FILE( student.h ) typedef char string9[10]; class Student// declares a class data type {// does not allocate memory public : // 5 public function members void AddCourse(string9 CourseName); void ListCourses(void) const; void ListInfo(void) const; void SetGradDate(int year); Student(); // Constructor Function Student(char name[ ], int year); private :// 4 private data members char studentName[30]; string9 courses[6]; int numCourses; int gradDate; } ;

7 Use of C++ data type class Facilitates re-use of C++ code for an ADT. Software that uses the class is called a client. Variables of the class type are called objects or instances of the class. Client code uses public member functions to handle its class objects. Private members cannot be directly accessed by client code. It can only be accessed by member functions.

8 #include "student.h" // includes specification of the class #include using namespace std; int main ( void ) { int newYear; string9 newCourse; Student newStudent;// create new Student object cout << "Enter graduation year: "; cin >> newYear; newStudent.SetGradDate(newYear); // set Graduation date cout << "Enter course to be added: "; cin >> newCourse; newStudent.AddCourse(newCourse); // Add a course newStudent.ListInfo(); // List Student information newStudent.ListCourses(); // List courses return 0; } // end of main Client Code Using Student

9 class type declaration The class declaration creates a data type and names the members of the class. It does not allocate memory for any variables of that type! Client code still needs to declare class variables.

10 C++ Data Type class represents an ADT 2 kinds of class members: data members and function members class members are private by default data members are generally private function members are generally declared public private class members can be accessed only by the class member functions, not by client code.

11 2 separate files generally used for class type // SPECIFICATION FILE ( student.h ) // Specifies the data and function members. class Student { public:... private:... } ; // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions....

12 Implementation file for Student // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions. #include "student.h" // also must appear in client code #include using namespace std;... void Student :: SetGradDate ( /* in */ int year ) { gradDate = year; }...

13 Class Constructors initialize Data Members // In the file student.cc Student :: Student () {//Default Constructor numCourses = 0; gradDate = 0; strcpy(studentName, "John Doe"); } // This is invoked in client code at time of object declaration: // Student newStudent("Mary Contrary", 2010); Student :: Student(char name[ ], int year) {//Constructor with parameters //We will fill this out in lab! }

14 Function stubs Stubs allow you to test code that relies on unwritten functions. The stub often has no code in the function definition: void Student :: AddCourse(string9 courseName) { //empty }

15 Function drivers Function drivers allow you to test class functions individually before writing the entire client code: int main (void) { Student newStudent; newStudent.listInfo( ); }