Computer Programming - C++ Lecture Notes PDF
Document Details
![IntricateAppleTree](https://quizgecko.com/images/avatars/avatar-5.webp)
Uploaded by IntricateAppleTree
Covenant University
Tags
Summary
These lecture notes provide an outline of computer programming concepts, focusing on diverse programming paradigms, including structure-oriented, event-driven, and object-oriented, as well as fundamental object-oriented design considerations and features crucial for C++ programming.
Full Transcript
Computer Programming Lecture Outline Comparison of Programming Language Paradigms Structure-oriented programming paradigms Event-driven programming paradigms Object-oriented programming paradigms. Fundamental of object oriented design. Features of object-oriented progr...
Computer Programming Lecture Outline Comparison of Programming Language Paradigms Structure-oriented programming paradigms Event-driven programming paradigms Object-oriented programming paradigms. Fundamental of object oriented design. Features of object-oriented programming. 2 Programming Paradigms are a way to classify programming languages according to the style of computer programming some languages fall into only one paradigm, while others fall into multiple paradigms 3 Common Paradigms Imperative Control flow is an explicit sequence of commands Procedural Imperative programming with procedure calls Declarative Programs state the result you want, not how to get it Structured Programs have clean, goto-free, nested control structures Functional Computation proceeds by (nested )function calls. Object Oriented Computation is effected by sending messages to objects; Object have state and behavior Event Driven control flow is determined by asynchronous actions (from human or sensors) Logic(Rule based) specifies a set of constraints, and an engine infers the answers to questions. ETC 4 Imperative programming paradigm Concerned with defining a linear procedure or sequence of programming statements. ▪ programs are written as lists of instructions for the computer to obey in sequence. ▪ It closely matches the computer's own sequential operation. 5 Example 1 (Imperative) Consider a program that creates customer bills for a small business. Assume that the business sells only one product that costs exactly N7.99. Each customer order must contain the desired quantity of the item and the customer’s name and address. If you could write the program in English rather than in a programming language, a simple version of the program might look something like 6 Procedural Programming Paradigm uses a list of instructions to tell the computer what to do step-by-step Imperative programming with procedure calls relies on – procedures(discrete re-usable modules ), also known as routines or subroutines. Object-Oriented programming also employ the principles of procedural programming. Examples of procedure-oriented languages are Fortran, ALGOL, COBOL, Ruby Programming Language. 7 Structure-oriented programming paradigm Consists of breaking big problems into smaller problems, then further breaking those into still smaller problems, and so on, until a level of such simplicity is reached that the implementation is obvious to the programmer expected to do the coding. An analogy to this is the divide and conquer method of solving a large programming task. 8 Example 2 (Structured version of Example 1) 9 Problems with Structured Programming No matter how well structured, large programs become excessively complex. Poor modelling of the real world. (Attributes and behaviour are separated) Can’t create new data type easily. (Not extensible) Unrestricted access to global data. 10 Event-driven programming paradigm program that waits until something happens and then performs a set of operations. ▪ Common events include you clicking on a mouse, moving a mouse over/out of some object on the screen, moving a mouse in general, typing something, until something If a user clicks a mouse button on any object then the click event occurs. If a user moves the mouse then the mouse move event occurs. It allows for more interactive programs. Almost all modern GUI programs use event driven programming. Examples : Visual Basic, Java etc 11 Object-oriented programming paradigm Tends to model the problem/world in terms of entities called objects that have attributes and behaviors (data and objects) and interact with other entities. ▪ You look around and you see a chair, a table, a person and the objects are related in one way or the other. An object developer defines a class or template for the entity which is instantiated or constructed and then may be used in the program. The object-oriented technique tries to imitate the way we think. 12 Fundamental idea of object oriented programming paradigm Combine both data and the functions that operate on that data into a single unit called object. An object’s functions (member functions in C++ or methods in other languages) provide the only way to access its data. The data is hidden, and therefore safe from accidental Alteration. Data encapsulation and data hiding. To access or modify an object’s data, we know exactly what functions interact with it. Calling an object’s member functions is referred to as sending a message to the object. 13 Object-oriented programming paradigm A program is structured as a web of interacting objects. Objects interact by sending messages to each other. OOP is an approach to the overall organization of the program. 14 15 Fundamentals/Features of object-oriented programming Components of OOP Objects Classes Instance variables Methods 16 Objects Objects model elements of the problem context Each object has: ▪ characteristics ▪ responsibilities (or required behaviors) Advantages - Major operation of a program can easily be isolated. - This helps to hide the details of the lower level objects 17 Example 3 Problem : Design and build a computer hockey game Object : Hockey player Characteristics: Position, height, weight, salary, number of goals Responsibilities: Pass the puck, shoot, skate forward, skate backward, punch another player, etc. 18 Example 4 Problem: Computation modeling in biology Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. 19 Example 4 contd. Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. What are objects? Characteristics? Responsibilities? 20 Solution Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive. 21 Solution Contd. Patient Virus Characteristics Characteristics reproduction rate (%) virus population resistance (%) immunity to virus (%) Responsibilities Responsibilities reproduce take drugs survive 22 Questions Why didn’t we model an object named Doctor? Surely, most hospitals have doctors, right? Doesn’t every patient have an age? Gender? Illness? Symptoms? Why didn’t we model them as characteristics? 23 CSC211 Computer Programming 1 2 Lecture Outline Variables Data types Constants Operators Basic Input and Output Dr. Isewon, Mr. Osofuye, Mr. Ogbu, Mr. Dada, Mr. Jegede, Miss. Nathaniel 3 Chapters 1&2 Dr. Isewon, Mr. Osofuye, Mr. Ogbu, Mr. Dada, Mr. Jegede, Miss. Nathaniel 4 Structure of a typical C++ program // my first program in C++ #include using namespace std; int main () { cout