Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Chapter 17-18

2 Defining Classes and Creating objects class 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İN2

3 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<<x.getAge()<<endl; x.setAge(7); cout<<x.getAge()<<endl; } CS116 SENEM KUMOVA METİN3

4 Access Functions class 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İN4

5 Constructors class 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İN5

6 Constructors class 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İN6

7 Constructor with default values class 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 assignment p4.print(); } CS116 SENEM KUMOVA METİN7

8 Constructors: Restricting Object Creation class 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İN8

9 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İN9

10 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 initializes Emp * cher= new Emp(“Cher”); // second constructor initializes return 0; } CS116 SENEM KUMOVA METİN10

11 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İN11

12 DESTRUCTOR: EXAMPLE class 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İN12

13 const keyword Where to use ?? – Input/ output parameters – Objects – Methods – Data members CS116 SENEM KUMOVA METİN13

14 const input or output parameters class 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İN14

15 const objects class 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İN15

16 const methods class 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İN16

17 const data members class 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İN17

18 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İN18 xxx c1 c2 c3 C::s

19 Static members : Data members A 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İN19

20 Static members : Methods A 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İN20

21 Static members : Accessing class 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 object Task::getN(); // access through class (direct access) unsigned x= c1.n; // access through an object unsigned y= c2.n; // access through an object unsigned z= Task::n; // access through class (direct access) } CS116 SENEM KUMOVA METİN21

22 Static variables defined inside methods class 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(); // 1 c2.m(); // 2 c3.m(); // 3 return 0;} CS116 SENEM KUMOVA METİN22

23 static const data members class 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İN23

24 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İN24

25 Passing and Returning Objects by Value class 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İN25 OUTPUT ???

26 Passing and Returning Objects by Reference class 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İN26 OUTPUT ???

27 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 getAge()<<endl; } void main() { Person x; x.setAge(4); cout<<x.getAge()<<endl; func(&x); cout<<x.getAge()<<endl;} CS116 SENEM KUMOVA METİN27  Accessing to an object’s members through a pointer requires class indirection operator “->” OUTPUT ???

28 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!!!!

29 Friend Function class 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();}

30 The Pointer Constant this class 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İN30

31 The Pointer Constant this class 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İN31

32 Using this pointer to enable cascaded function calls // Person.h class 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İN32

33 Using this pointer to enable cascaded function calls #include #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İN33


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

Similar presentations


Ads by Google