1 / 27

System Programming

System Programming. Practical Session 8. C++ classes. Outline Basic concepts Classes that hold pointers Destructor Copy constructor Operator=. C++ simple class example. // THE DECLARATION FILE OF THE CLASS POINT (Point.h) class Point { public : Point () ;

scarolyn
Download Presentation

System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Programming Practical Session 8 C++ classes

  2. Outline • Basic concepts • Classes that hold pointers • Destructor • Copy constructor • Operator=

  3. C++ simple class example • //THEDECLARATIONFILEOFTHECLASSPOINT(Point.h) • classPoint • { • public: • Point(); • Point(doublexval,doubleyval); • voidmove(doubledx,doubledy); • doublegetX()const; • doublegetY()const; • private: • double_x; • double_y; • };

  4. C++ simple class example • //THEIMPLEMENTATIONFILEOFCLASSPOINT(Point.cpp) • #include"Point.h" • Point::Point():_x(0),_y(0){} • Point::Point(doublexval,doubleyval):_x(xval),_y(yval){} • voidPoint::move(doubledx,doubledy){ • _x=_x+dx; • _y=_y+dy; • } • doublePoint::getX()const{ • return_x; • } • doublePoint::getY()const{ • return_y; • }

  5. C++ simple class example • Use example • #include<iostream> • #include“point.h” • intmain(){ • Pointp(0,0); • //Point p; • p.move(10,15); • constPointp2(20,12); • std::cout << p2.getx() << std::endl; • std::cout << p2.gety() << std::endl; • //p2.move(1,1);compilationerrorsincemoveisnotdeclaredconst • }

  6. Member Initialization List class Circle{ public: Circle(); Circle(double centerX, double centerY, double radius); ………….. private: Point _center; double _radius; }; Circle::Circle(): _center(0,0) , _radius(1) {} Circle::Circle(double centerX, double centerY, double radius): _center(centerX, centerY) , _radius(radius) {}

  7. Member Initialization List Example: Circle::Circle(): _center(0,0) , _radius(1) {} • Rules • The initial value can be any expression. • The order the initialization happens is according to the order the member variables are declared. • The member initialization list is executed before the body of the constructor. • Not initializing a member via the initialization list means implicitly calling its default constructor • Const members of a class can only be initialized via member initialization list.

  8. Inheritance Syntax: classDerived : publicBase{}; #include “point.h” #include “color.h” // suppose that we have defined class Color… class Pixel : public Point{ Color _color; …… }; Pixel::Pixel(Point point, Color color) : Point(point), _color(color) {} // example of a derived constructor calling the base constructor // Syntax:Derived::Derived(….) : Base(….) {…}

  9. Operator -> Shortcut for accessing members of an object pointed by a pointer. ptr->member is a shortcut for (*ptr).member • classPoint{.....} • intmain(){ • Point*p1=newPoint(0,0); • Pointp2(0,0); • p1->getX(); • (*p1).getX(); • p2.getX(); • (&p2)->getX(); • }

  10. Objects • In C++, object variables hold values, not object references. • When one object is assigned to another, a copy of the actual values is made. • When modifying an object in a function, you must remember to use call by reference. • Two object variables cannot jointly access one object. If you need this effect in C++, then you need to use pointers. • An object variable can only hold values of a particular type. If you want a variable to hold objects from different subclasses, you need to use pointers. • If you want a variable point to either null or to an actual object, then you need to use pointers in C++.

  11. Classes that hold pointers • A class that has a pointer data member should include the following member functions: • A virtual destructor • A copy constructor • operator= (assignment)

  12. Linked List Example Link data_ next_ List head_ data_ next_ data_ next_

  13. Link • classLink{ • private: • Link*next_; • std::stringdata_; • public: • Link(conststd::string&data,Link*link); • Link(constLink&aLink);//copy constructor • virtual~Link();// destructor • voidsetNext(Link*link); • Link*getNext()const; • conststd::string&getData()const; • };

  14. Link • Link::Link(conststd::string&data,Link*link):data_(data){ • setNext(link); • } • voidLink::setNext(Link*link){ • next_=link; • } • Link*Link::getNext()const{ • returnnext_; • } • conststd::string&Link::getData()const{ • returndata_; • } • Link::~Link(){}\\destructor • Link::Link(constLink&aLink){\\copy constructor • data_=aLink.getData(); • next_=0; • }

  15. List • classList{ • private: • Link*head_; • Link*copy()const; • voidclear(); • public: • List(); • constLink*getHead()const; • voidinsertData(conststd::string&data); • voidremoveFirst(); • List(constList&aList); • virtual~List(); • List&operator=(constList&L); • };

  16. List::List():head_(0){ } • constLink*List::getHead()const{ • returnhead_; • } • voidList::insertData(conststd::string&data){ • head_=newLink(data,head_); • } • voidList::removeFirst(){ • if(0!=head_){ • Link*tmp=head_; • head_=head_->getNext(); • deletetmp; • } • } • List::~List(){ • clear(); • } • voidList::clear(){ • while(0!=head_){ • removeFirst(); • } • }

  17. Link*List::copy()const{ • if(0==getHead())return0; • else{ • Link*head=newLink(*getHead()); • Link*next=head; • for(Link*origPtr=getHead()->getNext();0!=origPtr; • origPtr=origPtr->getNext()){ • next->setNext(newLink(*origPtr)); • next=next->getNext(); • } • returnhead; • }} • List::List(constList&aList){ • head_=aList.copy(); • } • List&List::operator=(constList&L){ • if(this==&L) • return*this; • clear(); • head_=L.copy(); • return*this; • }

  18. Destructor • An object's destructor function is called when that object is about to "go away“. • For local variables (objects declared on the stack) and value parameters – When the variable goes out of scope. • For dynamically allocated storage (objects on the heap) – • When the programmer frees the storage using delete.

  19. Destructor • voidf(ListL){ • List*p=newList(); • while(...){ • ListL1; • ... • } • deletep; • } Is a destructor function of a reference parameter called at the end of the function? No!

  20. /** • *Destructor:"deepdelete" • */ • List::~List(){ • clear(); • } • voidList::removeFirst() • { • if(0!=head_){ • Link*tmp=head_; • head_=head_->getNext(); • deletetmp; • } • } • voidList::clear(){ • while(0!=head_){ • removeFirst(); • } • }

  21. Copy Constructor • An object's copy constructor is called (automatically, not by the programmer) when it is created, and needs to be initialized to be a copy of an existing object. • This happens when an object is: • Passed as a value parameter to a function, • Pointq(2,2);Pointp(0,0); • p.moveTo(q);//moveTo(Point point) • Returned (by value) as a function result, • Declared with initialization from an existing object of the same class. • void f(Pointp){ • Pointtemp=p; • Pointtemp2(p); • …

  22. Example • Listf(ListL); • intmain(){ • ListL1,L2; • ... • L2=f(L1);//copyconstructorcalledheretocopyL1 • } • Listf(ListL){ • Listtmp1=L;//copyconstructorcalledheretocopyL • Listtmp2(L);//copyconstructorcalledheretocopyL • ... • returntmp1;//copyconstructorcalledhere • }

  23. Copy Constructor Declaration • classList{ • public: • …… • List(constList&L);//copyconstructor • ... • };

  24. Copy Constructor Definition • List::List(constList&aList){ • head_=aList.copy(); • } • Link*List::copy()const{ • if(0==getHead())return0; • else{ • Link*head=newLink(*getHead()); • Link*next=head; • for(Link*origPtr=getHead()->getNext(); • 0!=origPtr;origPtr=origPtr->getNext()){ • next->setNext(newLink(*origPtr)); • next=next->getNext(); • } • returnhead; • } • }

  25. Operator= • By default, class assignment is just a field-by-field assignment • If a class includes pointer fields, the default assignment operator causes aliasing which lead to trouble! Solution: overload operator= to perform deep copy. Syntax: List&operator=(constList&L);

  26. Operator= • Note that operator= differs from the copy constructor in three important ways: • The object being assigned to has already been initialized; therefore, if it has a pointer field, the storage pointed to must be freed to prevent a storage leak. • It is possible for a programmer to assign from a variable into itself; for example: L1 = L1. The operator= code must check for this case, and do nothing. • The operator= code must return a value

  27. Definition of operator= • It should always include the following 4 sections: • check assignment to self • clear existing data members • copy data member from other • return this • List&List::operator=(constList&L){ • if(this==&L){ • return*this; • } • clear(); • head_=L.copy(); • return*this; • }

More Related