Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS120: Introduction to Computer Science

Similar presentations


Presentation on theme: "CPS120: Introduction to Computer Science"— Presentation transcript:

1 CPS120: Introduction to Computer Science
Object-Oriented Concepts

2 The Procedural Paradigm
The functions and algorithms are the focus, with data viewed as something for the functions to manipulate

3 Structures Structures group variables together in order to make one's programming task more efficient. Any combination of variables can be combined into one structure. This is a useful and efficient way to store data. struct Student { string socSecNum; string lastName; string firstName; int pointsEarned; double gpa; };

4 Using the new data structure
The structure definition should be placed above the main function of a program but below the compiler directives Declare an actual variable of this programmer-created data type within a function (such as main) in order to make use of this structureDone with a declaration statement like Student freshmen; This reates a variable called freshmen of the data type Student Open struct2.cpp: #include<iostream.h> #include<iomanip.h> #include<string.h> struct inventory_item { char item_ID[11]; char description[31]; int quantity_on_hand; int reorder_point; double cost; double retail_price; }; int main() inventory_item todays_special; strcpy(todays_special.item_ID, "RGG "); strcpy(todays_special.description, "Remote Control Monster Truck"); todays_special.quantity_on_hand = 19; todays_special.reorder_point = 3; todays_special.cost = 47.80; todays_special.retail_price = 98.99; cout << "Today's Special \n"; cout << " Item ID: " << todays_special.item_ID << endl; cout << " Description: " << todays_special.description << endl; cout << " Quantity: " << todays_special.quantity_on_hand <<endl; cout << "Regular Price: " << setprecision(2) << todays_special.retail_price << endl; cout << " Sale Price: " << todays_special.retail_price * 0.8 << endl; return 0; }

5 Assigning values to the structure
To assign a grade point average (GPA) of 3.4 to the gpa member of the variable freshmen, use the statement: freshmen.gpa = 3.4; The period (.) that is used between the variable name freshmen and the member gpa is called the dot operator. The dot operator simply us to reference individual members of a structure struct Student { string socSecNum; string lastName; string firstName; int pointsEarned; double gpa; }; To assign a last name, you could use the statement, freshmen.lastName = "Smith";

6 Object-Oriented Paradigm
Data should be placed inside the objects and that these objects should communicate with each other in the form of messages

7 Designing a Class Think in an object-oriented way
E.g. an answering machine encapsulates the functions of an answering machine with the data (messages). The buttons are the equivalent of sending messages to the machine

8 Functionality of Object-Oriented Languages
Encapsulation Inheritance Polymorphism

9 Encapsulation Encapsulation is a language feature that enforces information hiding

10 Inheritance Inheritance fosters reuse by allowing an application to take an already-tested class and derive a class from it that inherits the properties the application needs

11 Polymorphism The ability of a language to have duplicate method names in an inheritance hierarchy and to apply the method that is most appropriate for the object which the method is applied

12 Object-Oriented Design
An object is a self-contained entity that makes sense within the context of the problem For example, a student

13 Object-Oriented Design
A group of similar objects is described by an object class, or class

14 OOP Advantages: Reusability
Reusability is a major benefit of object-oriented programming

15 Classes The definition of an object is know as a class
It is similar to using basic data structures in C++ When you declare an object, you are said to have instantiated it (given it instances) Objects are members of a class Paul Millis, George Bush and George Washington being members of the human being class The design of a class is as important as its implementation

16 Relationships Between Classes
Containment “part-of” An address class may be part of the definition of a student class Inheritance Classes can inherit data and behavior from other classes “is-a”

17 Public vs Private Private: Cannot be accessed outside the object
Public: Can have access to all the variables and functions public: // constructors circle(); // default constructor circle(const circle &); // copy constructor // member functions void SetRadius(float); double Area(); private: // data float radius;

18 Constructors Allow all data encapsulated within an object to be initialized to preset values so that errors can be avoided

19 Member Functions Provide a way for a programmer to pass data to and get data from an object Implemented like a normal C++ function Except -- The class name and the scope-resolution operator (: :) precede the function name circle : : circle() // default constructor


Download ppt "CPS120: Introduction to Computer Science"

Similar presentations


Ads by Google