chapter 17-18. defining classes and creating objects class person {public : void setage (int n)...

33
Chapter 17-18

Upload: christopher-brooks

Post on 17-Jan-2018

223 views

Category:

Documents


0 download

DESCRIPTION

Access Functions class Person { public : void setAge (unsigned n) { age = n; }; unsigned getAge() const {return age;}; private: unsigned age;}; /* Member functions(methods) which access private members are access functions */ main() {Person x; cout

TRANSCRIPT

Page 1: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Chapter 17-18

Page 2: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Defining Classes and Creating objectsclass Person { public : void setAge (int n) {age=n;}

int getAge() {return age;}

private:int age; };

main(){ Person p;

p.setAge(8);

Person & q=p;q.setAge(8);

Person * pointer=new Person;pointer->setAge(8);

Person arr[3];arr[0].setAge(8); arr[1].setAge(9);

arr[2].setAge(10); }CS116 SENEM KUMOVA METİN 2

Page 3: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Access Functionsclass Person { public : void setAge (unsigned n) { age = n; };

unsigned getAge() const {return age;};

private: unsigned age; };

/* Member functions(methods) which access private members are access functions */

main(){ Person x;

cout<<x.getAge()<<endl;

x.setAge(7);

cout<<x.getAge()<<endl;}

CS116 SENEM KUMOVA METİN 3

Page 4: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Access Functionsclass Time { public : void setTime (int h, int m) { this->h= h;

minute=m; }

int getHour();

int getMinute() {return minute;};

void print(){cout<<hour<<“:”<minute;}

private: int hour; int minute; };

int Time:: getHour(){return hour;}

main(){ Time x;

cout<<x.getHour()<<endl;

x.setTime(10,20);

cout<<x.getHour()<<endl;cout<<x.getMinute()<<endl;x.print();

}CS116 SENEM KUMOVA METİN 4

Page 5: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Constructorsclass Person { public : Person() {age=10;}

void setAge (unsigned n) { age = n; }; unsigned getAge() const {return age;};

private: unsigned age; };

main(){ Person x;

cout<<x.getAge()<<endl; // 10

x.setAge(7);

cout<<x.getAge()<<endl; //7}

CS116 SENEM KUMOVA METİN 5

Page 6: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Constructorsclass Person { public : Person() ;

Person(string n, int a) ; void print(){cout<<name<<“:”<<age; }

private: string name; int age; };

Person :: Person() { name =“Unknown”; age =0; }

Person :: Person (string n, int a) { name=n; age=a; }

void main(){ Person p1;

p1.print();

string x=“not name”;Person p2(x, 10);p2.print(); }

CS116 SENEM KUMOVA METİN 6

Page 7: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Constructor with default valuesclass Person { public : Person(string =“Unknown”, int

=0) ; void print(){ cout<<name<<“:”<<age; }

private: string name; int age; };

Person :: Person (string n, int a) { name=n; age=a; }

void main(){ Person p1;

p1.print(); string x=“not name”;Person p2(x, 10);p2.print();

Person p3(x);p3.print();

Person p4;p4=p3; // default memberwise assignmentp4.print(); }

CS116 SENEM KUMOVA METİN 7

Page 8: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Constructors: Restricting Object Creationclass Emp { public : Emp(unsigned ID ) { id=ID;}

unsigned id; private:Emp() ; };

void main(){

Emp cher(111222333); //IS IT POSSIBLE?? Emp elvis; //IS IT POSSIBLE??

}

/* IF A CLASS EXPLICITLY DECLARES ANY CONSTRUCTOR, THE COMPILER DOES NOT PROVIDE A public DEFAULT CONSTRUCTOR

*//* IF A CLASS DECLARES a NON public DEFAULT CONSTRUCTOR,

COMPILER DOES NOT PROVIDE A public DEFAULT CONSTRUCTOR

*/CS116 SENEM KUMOVA METİN 8

Page 9: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Copy Constructor A copy constructor creates a new object as a copy to another

object Copy constructor for Person class - Person(Person &);

class Person { public : Person(string n, int a) { name=n; age=a;}

private :string name; int age;};

main(){ string s1(“Bob”); Person p1(s1,15);

Person p2(p1); /* which constructor works????? */ }

You may define your copy constructor

class Person { public : Person(string , int) ;Person(Person & x) {name=x.name;

age=x.age+1;} ;private :

string name; int age;};

CS116 SENEM KUMOVA METİN 9

Page 10: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

CONSTRUCTORS : new and new [] operators

class Emp { public : Emp() {…..}Emp(const char * name) {…}

private : char *n; };

int main() {

int * x= new int;

Emp * elvis= new Emp(); // default constructor initializesEmp * cher= new Emp(“Cher”); // second constructor initializes

return 0;}

CS116 SENEM KUMOVA METİN 10

Page 11: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

DESTRUCTORS

A constructor is automatically invoked whenever an object is created

A destructor is automatically invoked whenever an object is destroyed

A destructor takes no arguments and no return type, there can be only one destructor per class

class C{

public :C() { … }; // constructor~C() { … }; // destructor

…}

CS116 SENEM KUMOVA METİN 11

Page 12: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

DESTRUCTOR: EXAMPLEclass C{ public :

C() {name=“anonymous”;} C(string n) { name=n;}~C() { cout <<“destroying” <<name<<“\n”;}

private:string name; };

int main()

{ string n = “hello”; C c1(n);

C * ptr =new C();delete ptr; // destructor for ptr object is called

return 0; // destructor for c1 is called }

CS116 SENEM KUMOVA METİN 12

Page 13: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

const keywordWhere to use ??

– Input/ output parameters

– Objects

– Methods

– Data members

CS116 SENEM KUMOVA METİN 13

Page 14: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

const input or output parametersclass Time { public:

void setTime( const int & m, const int& h ) { minute=m; hour=h; /* it is not possible for setTime to change the value of m or h since m and h are constants*/ }

const int & getHour() { return hour;};

private: int hour; int minute; };

main(){ int a=16, b=15;

Time obj;obj.setTime(a, b) ; /* setTime() cannot change

the value of a or b of main()*/const int &x = obj.getHour(); /* main() cannot change

the return value of getHour()hour is private */ }

CS116 SENEM KUMOVA METİN 14

Page 15: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

const objectsclass Time {  public: 

void setTime( int h, int m) {hour=h; minute=m};  void printStandard() const { cout<<hour<<“:”<<minute;};      

private:   int hour;     int minute;  };

main(){ Time wakeup;

wakeup.setTime(8,30);

wakeup.printStandard();

const Time noon;

noon.setTime(8,30); // noon is a constant object you can not call a // non_const method

noon.printStandard(); }

// const object may call only const methods

CS116 SENEM KUMOVA METİN 15

Page 16: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

const methodsclass Time { 

public:  void setTime( int, int );

void printUniversal()  const {hour=12; }; //??????         

void printStandard() const { cout<<hour;};      

private:   int hour;       int minute;       

};

/* The keyword const in methods printUniversal and printStandard

shows that unlike method SetTime, these methods do not change the

value of any Time data member… */

CS116 SENEM KUMOVA METİN 16

Page 17: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

const data membersclass Increment {  public: 

Increment(int c=0, int i=1);

void addIncrement {count+=incr;}         

void print const { cout<<count;};      

private:   int count;    

const int incr;  };

/* It is not possible to change the values of const data members after initialization, initialization can be done only in constructors*/

Increment:: Increment(int c, int i) :count(c),incr(i){};

// Increment:: Increment(int c, int i) :increment(i){count=c;};

CS116 SENEM KUMOVA METİN 17

Page 18: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Class Data Members and Methods• To create a class member static keyword is

used• A static (class) member is created once and it

is unique for all objects created from a class

class C { int x;

static int s; };

C c1, c2, c3;

CS116 SENEM KUMOVA METİN 18

x x x

c1 c2 c3

C::s

Page 19: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Static members : Data membersA static member does not effect the size of a class or an

object of this class type

A static data member may be declared inside a class declaration but must be defined outside

class Task{ public:

…private:

static unsigned n; // declaration…

};

unsigned Task::n=0; // definition

CS116 SENEM KUMOVA METİN 19

Page 20: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Static members : MethodsA static method can access only other static

members

class Task{ public:

static unsigned getN() const {return n;}static int getK() const {return k;} //NOT POSSIBLE!!!

private:static unsigned n; int k;

};

CS116 SENEM KUMOVA METİN 20

Page 21: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Static members : Accessingclass Task{ public:

static unsigned getN() const {return n;}static unsigned n;

};

unsigned Task::n=5;

int main(){ Task c1, c2;

c1.getN(); // access through an objectTask::getN(); // access through class (direct access)

unsigned x= c1.n; // access through an objectunsigned y= c2.n; // access through an object

unsigned z= Task::n; // access through class (direct access)}

CS116 SENEM KUMOVA METİN 21

Page 22: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Static variables defined inside methodsclass C { public : void m();

private : int x; };

void C::m(){ static int s=0; // one copy for all objects!!

cout<<++s<<‘\n’; }

int main(){ C c1,c2,c3;

c1.m(); // 1c2.m(); // 2c3.m(); // 3

return 0; }

CS116 SENEM KUMOVA METİN 22

Page 23: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

static const data membersclass Date {  public:  static const int monthsPerYear =12;

Date(int =1, int =1 , int =1900);  

void print const { cout<<month<<“.”<<day<<<<“.”year; }      

private:  int day; int month; int year;     };

Date:Date(int d, int m, int y){ day=d; year=y;

if(m>0&&m<=monthsPerYear) month=m;else { cout<<“invalid month”; month=1;} }

void main(){ Date obj1(12,4,1999);

obj1.print();

Date obj2(3,12,2000);obj2.print(); }

CS116 SENEM KUMOVA METİN 23

Page 24: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

How to define functions wtih objects ??

Call by value Send/return objects to/from functions

Call by reference Send/return references (or pointers) of objects to/from

functions

Friend Functions

CS116 SENEM KUMOVA METİN 24

Page 25: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Passing and Returning Objects by Valueclass Person { public : void setAge (unsigned n) { age = n; };

unsigned getAge() const {return age;};

private: unsigned age; };

Person func1(){ Person p;

p.setAge(4); return p; }

unsigned func2( Person y){ y.setAge(3);

return y.getAge(); }

main(){ Person x;

cout<<x.getAge()<<endl;

x=func1();cout<<x.getAge()<<endl;;

cout << func2(x)<<endl;cout<<x.getAge()<<endl; }CS116 SENEM KUMOVA METİN 25

OUTPUT???

Page 26: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Passing and Returning Objects by Referenceclass Person { public : void setAge (unsigned n) { age = n; };

unsigned getAge() const {return age;};private: unsigned age; };

Person & func1(){ static Person p;

p.setAge(4); return p; }unsigned func2( Person & y)

{ y.setAge(3);return y.getAge(); }

main(){ Person x;

cout<<x.getAge()<<endl;

x=func1();cout<<x.getAge()<<endl;;

cout << func2(x)<<endl;cout<<x.getAge()<<endl; }

CS116 SENEM KUMOVA METİN 26

OUTPUT???

Page 27: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Pointer to Objects

class Person { public : void setAge (unsigned n) { age = n; }; unsigned getAge() const {return age;};

private: unsigned age; };

void func(Person * ptr){ ptr->setAge(5); cout<<ptr->getAge()<<endl;}

void main(){ Person x;

x.setAge(4); cout<<x.getAge()<<endl;

func(&x);cout<<x.getAge()<<endl; }

CS116 SENEM KUMOVA METİN 27

Accessing to an object’s members through a pointer requires class indirection operator “->”

OUTPUT???

Page 28: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Friend Functions A class’s private members are accessible only to

1. its methods (member functions)2. its friend functions!!!

A friend function must be declared inside class definition with keyword “friend”

A friend function can be declared within private, protected or public part of the declaration of class

A friend function is not a method!!!!

Page 29: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Friend Functionclass Count { friend void setX(Count &, int); // friend function

public: Count ():x(0){}~Count() {cout<<“object is destructed”<<endl;}void print() { cout<<x<<endl;}

private: int x; };

void setX(Count &c, int val) {c.x=val;}

int main(){ Count counter;

cout<<“counter.x after instantiation : ”;counter.print();

setX(counter,8);cout<<“counter.x after call to setX friend function : ”;counter.print(); }

Page 30: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

The Pointer Constant thisclass C {public : C() {x=0;}

private: int x; };

IS SAME WITH

class C {public : C() {this->x=0;}private: int x; };

CS116 SENEM KUMOVA METİN 30

Page 31: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

The Pointer Constant thisclass Person{ public :

Person( string & name) { this->name=name; }

string getName() { return name; // return this->name

}private:

string name; };

void main(){ string n(“Joe”);

Person p(n);

cout<<n<<“ “<<p.getName(); }CS116 SENEM KUMOVA METİN 31

Page 32: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Using this pointer to enable cascaded function calls

// Person.hclass Person{ public :

Person( string & n, int a) { setPerson(n,a); }

Person & setPerson(string & n, int a) { setAge(a); setName(n);

return * this; /*enables cascading*/ }Person & setName(string & n) { name=n;

return * this; /*enables cascading */}Person & setAge(int a) { if(age>0) age=a;

else age=0; return * this; /*enables

cascading*/ }void print() {cout<< name<<“ –”<< age<<endl;}

private: string name; int age; };CS116 SENEM KUMOVA METİN 32

Page 33: Chapter 17-18. Defining Classes and Creating objects class Person {public : void setAge (int n) {age=n;} int getAge() {return age;} private:int age;};

Using this pointer to enable cascaded function calls

#include<iostream>#include “Person.h”using namespace std;

int main(){ Person p;

string s=“Mary”;string q=“Joe”;

p.setName(s).setAge(10); p.print();

p.setPerson(q,12).print();

}

CS116 SENEM KUMOVA METİN 33