Question? Leave a message!




Procedural Programming vs OOP

Procedural Programming vs OOP
Dr.MasonHanks Profile Pic
Dr.MasonHanks,Germany,Teacher
Published Date:23-07-2017
Website URL
Comment
CIS 190: C/C++ Programming Lecture 8 Classes in C++ 1 Outline • Procedural Programming vs OOP • Classes – Example: Morphing from Struct – Basics – Access – Constructors – Overloading • Livecoding 2 Procedural Programming • up until now, everything we’ve been doing has been procedural programming • code is divided into multiple procedures – procedures operate on data (structures), when given correct number and type of arguments • examples: PrintTrain(), ReadSingerFile(), DestroyList(), ProcessEvents(), etc. 3 Object-Oriented Programming • now that we’re using C++, we can start taking advantage of object-oriented programming • adding OOP to C was one of the driving forces behind the creation of C++ as a language – C++’s predecessor was actually called “C with Classes” 4 Object-Oriented Programming • in OOP, code and data are combined into a single entity called a class – each instance of a given class is an object of that class type • principles of Object-Oriented Programming – encapsulation – inheritance – polymorphism 5 OOP: Encapsulation • encapsulation is a form of information hiding and abstraction • data and functions that act on that data are located in the same place (inside a class) • ideal: separate the interface/implementation so that you can use the former without any knowledge of the latter 6 OOP: Inheritance • inheritance allows us to create and define new classes from an existing class • this allows us to re-use code – faster implementation time – fewer errors – easier to maintain/update 7 OOP: Polymorphism • polymorphism is when a single name can have multiple meanings – normally used in conjunction with inheritance • we’ll look at one form of polymorphism today: – overloading functions 8 Outline • Procedural Programming vs OOP • Classes – Example: Morphing from Struct – Basics – Access – Constructors – Overloading • Livecoding 9 Example: Date typedef struct date int month; int day; int year; DATE; 10 Parts of a Struct typedef struct date name of the struct int month; int day; int year; DATE; 11 Parts of a Struct typedef struct date name of the struct int month; int day; int year; DATE; (optional) shorter name via typedef 12 Parts of a Struct typedef struct date name of the struct int month; int day; member variables of the structure int year; DATE; (optional) shorter name via typedef 13 Using a Struct • if we want to print a date using the struct, what should our function prototype be? ____ PrintDate(________); 14 Using a Struct • if we want to print a date using the struct, what should our function prototype be? void PrintDate(DATE day); 15 Using a Struct • if we want to print a date using the struct, what should our function prototype be? void PrintDate(DATE day); • if we want to change the year of a date, what should our function prototype be? ____ ChangeYear(__________________); 16 Using a Struct • if we want to print a date using the struct, what should our function prototype be? void PrintDate(DATE day); • if we want to change the year of a date, what should our function prototype be? void ChangeYear(DATE day, int year); 17 Morphing from Struct to Class typedef struct date int month; int day; int year; DATE; 18 Morphing from Struct to Class struct date int month; int day; int year; ; • remove the typedef – we won’t need it for the class 19 Morphing from Struct to Class class date int month; int day; int year; ; • change struct to class 20