Presentation is loading. Please wait.

Presentation is loading. Please wait.

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;

Similar presentations


Presentation on theme: "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;"— Presentation transcript:

1 PART I CHAPTER 16 CS116 SENEM KUMOVA METİN 1

2 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

3 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

4 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

5 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 ; }

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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; }


Download ppt "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;"

Similar presentations


Ads by Google