Unformatted text preview:

C++ & Object-Oriented ProgrammingA class is a unit of encapsulationA class is an abstractionClasses and C structuresClassesClasses and structuresClasses: writing set and get functionsClasses and ObjectsSlide 9Slide 10Classes: constructorsConstructor parameters:Constructors/destructorsPrefer initialization to assignmentPrefer init to assignPassing param to base classList members in init list in order that they are declaredExercise to learn constructorsWhy is this a bad class definition? at least 2 member functions missingQ: what output should we get:Copying a class objectSlide 22Canonical orthodox class form J. CoplienWhat functions does C++ silently write?How would you need them?What do the compiler generated functions look like?When do we need to write a copy constructor, destructor & assign operator?How do we overload assignment?Formula for overloaded assignmentan overloaded binary operatorPrinciple of least privilegeExplicitly disallow any implicit functions you don’t want!Overloading operatorsoverloading output operator: as a friend functionOverloading output operator: as stand-alone functionPut prototype in header file, and body in implementation fileExercise with classes:Using studentsWhat’s missing:FriendsOverloading the output operatorSlide 42Slide 43Class exercise:Discussion of Manager class:Suggested Naming ConventionsOperator Overloading: ADT for binary mathBinary should “look like” intSlide 49Slide 50Slide 51Exercise for Binary type:Discussion of Binary classmakefile/MakefileSlide 55Discussion of makefileC-strings are no fun:Solution: write our own String class What do we need:Dynamic storageSlide 60Slide 61Slide 62We need operator==String exercise:Slide 65Slide 66Discussion of String::operator=Slide 68Slide 69Making Strings useful:Iterator classiteratorSlide 73StringIterator spec discussionSlide 75Slide 76StringIterator exercises:Stack: a common data structureSlide 79Stack exercise:Template classesSlide 82Discussion of template stackExceptionsHow to use exceptionsSlide 86Slide 87Slide 88Stack exerciseSlide 90Linked listSingly linked listSlide 93Slide 94Slide 95Discussion of List classList class exerciseList exerciseDoubly linked listDoubly linked listSlide 101Slide 102Slide 103Doubly linked list exercisesC++ & Object-Oriented ProgrammingThe Class2A class is a unit of encapsulationPublic operations, andprivate implementation.3A class is an abstractionStringString abstracts the char * of C,stackstack -- the canonical abstractionListStudent,etc.4Classes and C structuresdefault access for class: privatedefault access for struct:public5ClassesExample of a bad class declaration class Student {public: float gpa; char * name;};6Classes and structuresCorrect example of Class Declaration class Student {private: float gpa; char * name;};do we need the “private” designation?7Classes: writing setset and getget functions class Student {public: void setGpa(float g) { gpa = g; } void setName(char * n) { name = new char[strlen(n)+1]; strcpy(name, n); } private: float gpa; char * name;};8Classes and ObjectsDeclaration of a class variable (i.e. object)Student s, t;Student * v;int i;float x; The syntax is identical to conventional variable declarations.9ClassesSince class declarations will likely be used by many program modules, put them in a header file that can be included as required#include “Student.h”10Classes and ObjectsAccess to class data memberss.setGpa(3.5);s.setName(“Dana”);v = new Student(4.0, “Fox”);t = s; The Student class also needs constructors11Classes: constructors class Student {public: Student(); //default Student(char*, float); //conversion Student(const Student&); //copy friend ostream & operator<<(ostream &, const Student &);private: char * name; float gpa;};12Constructor parameters:Default: no parametersConversion: parameters to make oneCopy: the parameter is the copy instance13Constructors/destructorsGuarantee that data is initializedallow classes to manage memoryconstructors allocate: newdestructors deallocate: delete14Prefer initialization to assignmentclass Student {public: Student(int a) : age(a), iq(age+100) {} Student(int a) { age = a; iq = age + 100; }private: int age; int iq;};15Prefer init to assignInitialization is more efficient for data members that are objects:init: uses copy constructorassign: uses default constructor and assignmentonly way to pass a parameter to base class16Passing param to base classclass Person {public: Person(int a) : age(a) {}private: int age;};class Student : public Person {public: Student(int age, float g) : Person(age), gpa(g) {}private: float gpa;};17List members in init list in order that they are declaredclass members are initialized in the order ofclass members are initialized in the order oftheir declaration in the classtheir declaration in the class! What’s the value of iqWhat’s the value of iq?class Student {public: Student(int a) : age(a), iq(age+100) {}private: int iq; int age;};18Exercise to learn constructorsWrite 3 constructors and a destructor for Student class. Test it with:void fun(Student stu) {}int main() { Student a, b(“Darth Maul”, 3.5), c = b; Student * d = new Student(“Anakin”, 4.0); cout << *d << endl; fun(a); return 0;}19Why is this a bad class definition?at least 2 member functions missing class Student {public: Student(); //default Student(char*, float); //conversion Student(const Student&); //copy friend ostream & operator<<(ostream &, const Student &);private: char * name; float gpa;};20Q: what output should we get:int main() { Student x(“Count Dooku”), y(“Anakin”); x = y; y.setName(“Darth Maul”); cout << x << endl;}Soln: need deep copy: operator=21Copying a class objectOriginal ObjectShallowCopy22Copying a class objectOriginal ObjectDeepCopy23Canonical orthodox class form J. CoplienDefault constructorcopy constructordestructorassignment operator24What functions does C++silently write?class Empty{};class Empty {public: Empty(); Empty(const Empty &); ~Empty(); Empty& operator=(const Empty &); Empty * operator&(); const Empty * operator&() const;};25How would you need them?const Empty e1; // need default constructor, and// destructorEmpty e2(e1); // copy constructore2 = e1; // assignment operatorEmpty * pe2 = &e2; // address-of


View Full Document

Clemson CPSC 870 - Lecture

Documents in this Course
Load more
Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?