CPS120: Introduction to Computer Science Object-Oriented Concepts
The Procedural Paradigm The functions and algorithms are the focus, with data viewed as something for the functions to manipulate
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; };
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, "RGG456-299"); 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; }
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";
Object-Oriented Paradigm Data should be placed inside the objects and that these objects should communicate with each other in the form of messages
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
Functionality of Object-Oriented Languages Encapsulation Inheritance Polymorphism
Encapsulation Encapsulation is a language feature that enforces information hiding
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
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
Object-Oriented Design An object is a self-contained entity that makes sense within the context of the problem For example, a student
Object-Oriented Design A group of similar objects is described by an object class, or class
OOP Advantages: Reusability Reusability is a major benefit of object-oriented programming
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
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”
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;
Constructors Allow all data encapsulated within an object to be initialized to preset values so that errors can be avoided
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