🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

MeticulousTrigonometry1356

Uploaded by MeticulousTrigonometry1356

University of Mines and Technology

2022

Tags

object oriented programming C++ computer science

Full Transcript

UNIVERSITY OF MINES AND TECHNOLOGY TARKWA FACULTY OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LECTURE NOTE CE/EL 162 OBJECT ORIENTED PROGRAMMING WITH C++ COMPILED BY Dr William...

UNIVERSITY OF MINES AND TECHNOLOGY TARKWA FACULTY OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LECTURE NOTE CE/EL 162 OBJECT ORIENTED PROGRAMMING WITH C++ COMPILED BY Dr William Akotam Agangiba & Ezekiel M. Martey July, 2022 i TABLE OF CONTENTS Contents TABLE OF CONTENTS............................................................................................................. ii d) Stroustrup................................................................................................................................ 1 Course Presentation...................................................................................................................... 2 Course Assessment......................................................................................................................... 2 Attendance...................................................................................................................................... 2 CHAPTER ONE- CLASSES AND OBJECTS.......................................................................... 3 1.1 C++ Class Definitions..................................................................................................... 3 1.2 C++ Objects..................................................................................................................... 3 1.3 Accessing the Data Member............................................................................................ 4 1.4 Classes and Objects in Detail.......................................................................................... 5 Class member Function.......................................................................................................... 5 Class Access Modifiers........................................................................................................... 8 Class Constructor and Destructor........................................................................................ 13 Copy Constructor.................................................................................................................. 17 Inline Functions.................................................................................................................... 22 this Pointer............................................................................................................................ 23 Pointer to C++ Classes......................................................................................................... 24 Static Members of a Class.......................................................................................... 26 1.5 Creating Dynamic Objects................................................................................................... 29 CHAPTER TWO - OOP CONCEPTS..................................................................................... 31 2.1 Data Encapsulation In C++.......................................................................................... 31 2.2 C++ Inheritance............................................................................................................ 33 2.3 Polymorphism In C++................................................................................................. 38 Virtual Function.................................................................................................................... 41 Pure Virtual Functions......................................................................................................... 41 Friend Functions................................................................................................................... 42 2.4 Data Abstraction In C++............................................................................................... 43 CHAPTER THREE - INTERFACES IN C++ (ABSTRACT CLASSES)............................. 47 CHAPTER FOUR - C++ OVERLOADING (OPERATOR AND FUNCTION).................. 50 ii 4.1 Function Overloading in C++....................................................................................... 50 4.2 Operators Overloading in C++..................................................................................... 51 4.3 Overloadable/Non-overloadableOperators................................................................... 54 4.4 Operator Overloading Examples.................................................................................. 54 iii Course Objective This course is designed to help students understand Object-oriented Programming concepts and techniques, the principles of software engineering in Object-oriented languages, the fundamentals of programming in C++. Students will be able to design and implement Object-oriented software to solve moderately complex problems, and to write good program documentation. Course Content: Object oriented paradigm & C++ at a glance, Classes and objects, Object initialization and cleanup, Dynamic objects, Operator overloading, Inheritance, Virtual functions, Generic programming with templates, Streams computation with streams, Stream computation with files, Exception handling. Reference Materials: a) Bjarne, S (2013), “Programming: Principles and Practice Using C++” Addison Wesley, 2nd edition, ISBN-13: 978-0321992789 b) Deitel, P. and Deitel, H. (2013), “C++ How to Program”, Pearson Education Limited, England, 8th edition, 1101pp. c) Malik, D. S. (2013), C++ Programming: From Problem Analysis to Program Design, 7th edition, Cengage Learning, ISBN-13: 978-1285852744 d) Stroustrup, B., (2013), The C++ Programming Language, Pearson Education Limited , 4th edition, ISBN-13: 978-0321563842 1 Course Presentation The course is presented through lectures and Laboratory Hands-on Practice supported with a hand- out. During the Lab sessions, students are guided to solve practical problems with varying degrees of difficulty. Students are also given practical exercise to solve on their own and submit solutions in the form of assignments. The student can best understand and appreciate the subject by attending all lectures and laboratory work, by practising, reading references and hand-outs and by completing all assignments on schedule. Course Assessment Factor Weight Location Date Time To Be Announced Quizzes (3) 15 % In class 1 Hour/Each Grading (TBA) System Assignments 15% SD Attendance 10 % In class Random Practical To Be Announced Final Exam 60 % (TBA) (2hrs) (TBA) Written (2hrs) 80-100% A 70-79.9% B 60-69.9% C 50-59.9% D 0-49.9% FAIL Attendance According to UMaT rules and regulations, attendance is mandatory for every student. A random attendance shall be taken to constitute 10% of the total semester mark. The only acceptable excuse for absence is one authorized by the Dean of Students on their prescribed form. However, a student can also ask permission from me to be absent from a particular class with a tangible reason. A student who misses more than 50% of the total attendance marked WOULD not be allowed to take the final exams. 2 CHAPTER ONE- CLASSES AND OBJECTS The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. Placing data and functions together into a single entity is the central idea of object-oriented programming. This is done with the help of a class. A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class. 1.1 C++ Class Definitions When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object. A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the keyword class as follows: class Box { public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; The keyword public determines the access attributes of the members of the class that follows it. A public member can be accessed from outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which we will discuss in a sub-section. 1.2 C++ Objects A class provides the blueprints for objects, so basically an object is created from a class. An object has the same relationship to a class that a variable has to a data type. An object is an instance of a class, in the same way that a goat is an instance of a domestic mammal. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box – Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box 3 1.3 Accessing the Data Member The public data members of objects of a class can be accessed using the direct member access operator (.). Let us try the following example to make the things clear: #include using namespace std; class Box { public: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box }; int main() { Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box double volume = 0.0; // Store the volume of a box here // box 1 specification Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // box 2 specification Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // volume of box 1 volume = Box1.height * Box1.length * Box1.breadth; cout

Use Quizgecko on...
Browser
Browser