2 BytesC++ course_2014_c5_ structures and classes

Post on 04-Aug-2015

29 views 1 download

Tags:

transcript

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Summer course- 2014

2 bytes team

Welcome guys !

Smashing C++ !!

Classes & Structures

Structures !

Structures

• Holds pieces of data

Structures

• Holds pieces of data

Structures

• Holds pieces of data

struct structName{ ……….members …….. } its objects(if any) ;

Structures

• A member could be from a struct type .

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

Structures

• A member could be from a struct type .

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

You can define Like this or like this : Date x,y ;

Structures

• A member could be from a struct type .

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

Don’t forget comma !

Structures

struct structName{ ……….members …….. } its objects(if any) ; ….. …… int main() { …. }

• This declaration must be Global , to be seen by all functions.

Structures

• It’s a type ! So it means :

struct Date{ int month; int day; int year; }

void get(Date x, int count )

Structures

struct Date{ int month; int day; int year; };

Date get (int z , int count )

• It’s a type ! So it means :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

Structures

PersonInfo christoph ; christoph.height=170; christoph.weight=70; christoph.birthday.month=10 ; christoph.birthday.day=3 ; christoph.birthday.year=1978 ;

• To reach to its members :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

Structures

PersonInfo christoph ; christoph.height=170; christoph.weight=70; christoph.birthday.month=10 ; christoph.birthday.day=3 ; christoph.birthday.year=1978 ;

• To reach to its members :

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height • weight

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height • weight • birthday

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height • weight • birthday

• month • day • year

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height • weight • birthday

• month • day • year

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height • weight • birthday

• month • day • year

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height • weight • birthday

• month • day • year

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

• Like Arrays

• Sequentially

• PersonInfo

• height • weight • birthday

• month • day • year

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70, 10,3,1978};

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70, 10,3,1978};

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70, 10,3,1978};

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70, 10,3,1978};

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70, 10,3,1978};

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70};

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70};

month=0; day=0; year=0;

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

PersonInfo christoph ={170,70};

month=0; day=0; year=0;

Structures

• Initialize :

struct Date{ int month; int day; int year; } x,y ; struct PersonInfo { double height;//in cm int weight;//in kg Date birthday; };

Note :

Unlike Array , when you initialize , if you pass the number of members !

You will have an Error !

& To save the memebers values !

Output:

Classes !

Classes

• It’s like a Struct.

• Its members could be Functions and data

• it supports( information hiding, data abstraction, and encapsulation )

Classes

• It’s like a Struct.

• Its members could be Functions and data

• it supports( information hiding, data abstraction, and encapsulation )

• private, only seen in the current class(for class functions) , unseen out of class !

• public, seen everywhere (in current class and out of class)

• We use two things in classes :

Classes

• It’s like a Struct.

• Its members could be Functions and data

• it supports( information hiding, data abstraction, and encapsulation )

• Mostly in each class , you might have get(privateto variables )and set(variablesprivate) functions

• private, only seen in the current class(for class functions) , unseen out of class !

• public, seen everywhere (in current class and out of class)

• We use two things in classes :

Classes

• Mostly in each class , you might have get(privateto variables )and set(variablesprivate) functions

Classes

• Mostly in each class , you might have get(privateto variables )and set(variablesprivate) functions

Accessor functions Mutator functions

Classes

class className{ private: ……….members …….. public: ……….members …….. } ;

Classes

class className{ private: ……….members …….. public: ……….members …….. } ;

Don’t forget!

• You can definitions them here !

• But it’s better not !

Returned type

Class name

“ : : “

• Output:

Classes

class className{ private: ……….members …….. public: ……….members …….. } ;

Classes

class className{ int num,den; public: ……….members …….. } ;

Classes

struct structName{ int num,den; …members …….. } ;

Classes

struct structName{ int num,den; …members …….. } ;

That’s for today

We’re finished !!

2 bytes team

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team