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;

Slides:



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

Structure.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Structures Spring 2013Programming and Data Structure1.
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
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
Senem KUMOVA METİN CS FALL 1 ARRAYS && SORTING && STRINGS CHAPTER 6 cont.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 CS 201 Introduction to c++ (1) Debzani Deb. 2 History of C++ Extension of C (C++ for better C) Early 1980s: Bjarne Stroustrup (Bell Laboratories) Provides.
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.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
Introduction to Classes and Data Abstraction
 2003 Prentice Hall, Inc. All rights reserved Introduction Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Strings Sujana Jyothi C++ Workshop Day 4. A String also called character string is a sequence of contiguous characters in memory terminated by the NUL.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
CLASS INHERITANCE WEEK 7 Chapter 20 CS SENEM KUMOVA METIN1.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Learners Support Publications Classes and Objects.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
1 Classes and Data Abstraction Part I Introduction Object-oriented programming (OOP)  Encapsulates data (attributes) and functions (behavior)
1 Classes and Data Abstraction 2 Objectives Understand encapsulation and data hiding Understand data abstraction and Abstract Data Types (ADTs) Create.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 6: Classes and Data Abstraction Outline 6.1Introduction 6.2Structure Definitions 6.3Accessing.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
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.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Structures (L30) u Syntax of a struct Declaration u Structure Variables u Accessing Members of Structures u Initialize Structure Variables u Array of Structures.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Classes and Data Abstraction Andrew Davison Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University 1
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?
1 Lecture 6 Classes and Data Abstraction: Part II.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 6: Classes and Data Abstraction Posted Feb 3 Chapter 6 pointer for function Class Introduction.
Chapter Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};
1 Classes classes and objects - from object-oriented programming point of view class declaration class class_name{ data members … methods (member functions)
Lecture #6 Classes and Objects.
Classes.  A collection of variables combined with a set of related functions MemberFunctions (methods) (methods) MemberVariables (data members) (data.
C Structs Programming in C++ Fall 2008 Dr. David A. Gaitros
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
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.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
Chapter 9 Classes: A Deeper Look, Part 1 Seventh Edition C++ How to Program © by Pearson Education, Inc. All Rights Reserved.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
1 Example Original Array Array After 2 nd Pass Array After 1 st Pass Array After 3 rd Pass.
Topic 4 Data Structures Program Development and Design Using C++, Third Edition.
Chapter 6 Data Structures Program Development and Design Using C++, Third Edition.
Classes and Data Abstraction
Chapter 16: Classes and Data Abstraction
CS SUMMER LECTURE 1 by İlker Korkmaz.
Chapter 5 Classes.
CS1201: Programming Language 2
Classes: A Deeper Look Outline
An Introduction to Java – Part II
Classes and Objects.
Object Oriented Programming in java
CS1201: Programming Language 2
Chapter 6: Classes and Data Abstraction
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

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; int minute; }; Members of the same structure must have unique names Two different structures may contain members of the same name struct st1{ int n; }; struct st2{ float n; }; Each structure definition must end with a semicolon CS116 SENEM KUMOVA METİN2

Creating Objects from A Structure in C++ struct Time { int hour; int minute; }; /*1*/ Time timeObject; /*2*/ Time timeArray[ 10 ]; /*3*/ Time *timePtr1; timePtr1= &timeobject; /*4*/ Time *timePtr2; timePtr2=new Time; /*5*/ Time &timeRef= timeObject; CS116 SENEM KUMOVA METİN3

Accessing Members of Structures Member access operators: Dot operator (.) for structures and objects Arrow operator (->) for pointers Print member hour of timeObject: Time timeObject; Time & timeRef= timeObject; cout << timeObject.hour; cout << timeRef.hour; OR Time * timePtr; timePtr = &timeObject; cout hour; // timePtr->hour is the same as ( *timePtr ).hour CS116 SENEM KUMOVA METİN4

EXAMPLE: Structure #include using namespace std; struct Time { int hour; // 0-23 int minute; // 0-59 }; void print(const Time & ); int main() { Time dinnerTime; dinnerTime.hour = 18; dinnerTime.minute= 30; cout<<endl; print( dinnerTime ); } CS116 SENEM KUMOVA METİN5 void print( const Time &t ) { cout <<setfill(‘0’)<<setw(2) <<t.hour <<“:”<< setw(2) <<t.minute ; }

STRUCTURES : No Information Hiding struct Time { int hour; int minute; }; Time x; x.hour=23; x.minute =54; All members of the structure are accessible!! All members are public NO INFORMATION HIDING CS116 SENEM KUMOVA METİN6

CLASSES A class is a data type Model objects that have both attributes (data members) behaviors (member functions -methods) Have a body delineated with braces ({ and }) Class definitions terminate with a semicolon class Time { /* data members and methods */}; CS116 SENEM KUMOVA METİN7 class tag keyword

Creating objects from classes class Time { // data members and methods …… }; /*1*/ Time dinnertime; // Creates an object of type Time /*2*/ Time array[ 5 ]; // array of Time objects /*3*/ Time *pointer1; // pointer to a Time object pointer1=&dinnertime; /*4*/ Time *pointer2= new Time; // pointer to a Time object /*5*/ Time &dinnerTime = dinnertime; // reference to a Time object CS116 SENEM KUMOVA METİN8

Information Hiding in C++ In C ++ classes can limit the access to their members and methods The three types of access a class can grant are: public : this keyword can be used to expose data members and methods (make accessible wherever the program has access to an object of the class ) private : this keyword can be used to hide data members and methods (make accessible only to member methods of the class ) protected : Similar to private ( will study it later!!) CS116 SENEM KUMOVA METİN9

Class Declaration class Time { public: void setTime( int, int ); // sets hour,minute public: void print(); // prints time private: int hour; // 0 – 23 int minute; // 0 – 59 }; // Time class consists of public methods and private data members // A colon : follows the keywords private and public CS116 SENEM KUMOVA METİN10

const keyword class Time { public: void setTime( int, int); // sets hour,minute void print() const; // prints time private: int hour; // 0 – 23 int minute; // 0 – 59 }; /* The keyword const in methods “print” shows that unlike method SetTime, these methods do not change the value of any Time data member… */ CS116 SENEM KUMOVA METİN11

Member selector operator Access to any class member, whether data member or method, is supported by the member selector operator “.” and the class indirection operator “->” class Time { public: void setTime( int h, int m); // sets hour,minute void print() const; // prints time private: int hour; // 0 – 23 int minute; // 0 – 59 }; main() { Time noww; // declare an object from class Time noww.setTime (11,30); noww.minute =34; // IS it possible ????? } CS116 SENEM KUMOVA METİN12

Class Scope class C {public : void m(); // public scope private : char d; // private scope == class scope int f(); }; // If no public and private keywords are used then the members are default in private scope class D { int x; } ; equals to class D { private : int x; }; CS116 SENEM KUMOVA METİN13

Defining Class Methods 1. A method can be defined inside the class declaration. Such a definition is said to be “inline” 2. A method can be declared inside the class declaration but can be defined outside the class declaration CS116 SENEM KUMOVA METİN14

Defining Class Methods 1 class Person { public : void setAge (int n) { age = n; }; // inline definition int getAge() const {return age}; private: int age; }; CS116 SENEM KUMOVA METİN15

Defining Class Methods 2 class Person { public : void setAge (int n); // declares the method int getAge() const; private: int age; }; // definitions for methods void Person :: setAge(int n) { age = n; } int Person :: getAge() const { return age; } CS116 SENEM KUMOVA METİN16

Using Classes in a Program #include using namespace std; class Person {public : void setAge (int n) { age = n; }; int getAge() const {return age;}; private: int age;}; void main() { Person p; p.setAge(12); cout <<p.getAge()<<endl; // p.age=13; ??????? Person student [2]; student[0].setAge(12); student[1].setAge(15); for (int i=0; i<2;i++) cout<<student[i].getAge()<<endl;} CS116 SENEM KUMOVA METİN17

Initializing Class Objects: Constructors Method that initializes class members Same name as the class No return type Member variables can be initialized by the constructor or set afterwards CS116 SENEM KUMOVA METİN18

Constructors : inline definition class Person { public : Person() { age =0; name =“Unknown”; } void setAge (int n) { age =n }; int getAge() const { return age }; void getName() const { cout <<name<<endl; } private: int age; string name; }; void main() { Person p; cout <<p.getAge()<<endl; cout <<p.getName()<<endl; } CS116 SENEM KUMOVA METİN19

Constructors class Person { public : Person() ; void setAge (int n) { age =n }; int getAge() const { return age }; void getName() const { cout <<name<<endl; } private: int age; string name; }; Person ::Person() {age =0; name =“Unknown”; } void main() { Person p; cout <<p.getAge()<<endl; cout <<p.getName()<<endl; } CS116 SENEM KUMOVA METİN20

Constructors : Overloading class Person { public : Person() { age =0; name =“Unknown”; } // First Constructor Person( const string & n) { name =n; } // Second Constructor void setAge (int n) { age =n; }; int getAge() const { return age }; void getName() const { cout <<name<<endl; } private: int age; string name;}; void main() {Person q; // USING FIRST CONSTRUCTOR cout <<q.getAge()<<endl; cout<< q.getName()<<endl Person p(“David”); // USING SECOND CONSTRUCTOR cout <<p.getAge()<<endl; cout <<p.getName()<<endl; } CS116 SENEM KUMOVA METİN21

AN EXAMPLE FROM YOUR TEXT BOOK #include using namespace std; class GradeBook { private: string courseName; public : void setcourseName (string n) { courseName=n; } string getcourseName() { return courseName;} GradeBook(string name) { setcourseName(name);} void displayMessage(){ cout<<“Welcome”<<getcourseName(); } }; void main() { GradeBook book1(“CS116 Int. To Programming II”); cout <<book1.getcourseName()<<endl; } CS116 SENEM KUMOVA METİN22

AN EXAMPLE FROM YOUR TEXT BOOK //GradeBook.h #include using namespace std; class GradeBook { private: string courseName; public : void setcourseName (string n) { courseName=n; } string getcourseName() { return courseName;} GradeBook(string name) { setcourseName(name);} void displayMessage() { cout<<“Welcome”<<getcourseName(); } }; CS116 SENEM KUMOVA METİN23 #include #include “GradeBook.h” using namespace std; void main() { GradeBook book1(“CS116”); cout <<book1.getcourseName()<<endl; }