1 C++ Classes & Object Oriented Programming Overview & Terminology.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

C++ Classes & Data Abstraction
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
More about classes and objects Classes in Visual Basic.NET.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OBJECT ORIENTED PROGRAMMING
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Chapter 12: Adding Functionality to Your Classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
1 Chapter 14 Object-Oriented Software Development.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Variable Scope Storage Class Recursion
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
C++ Classes How to Create and Use Them. 2 Overview Terminology File Topology Designing Classes Functions in Classes (methods)  Constructor  Accessors/Modifiers.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Programming in Java CSCI-2220 Object Oriented Programming.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Abstraction ADTs, Information Hiding and Encapsulation.
1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!
Object Oriented Programming
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Overview of C++ Polymorphism
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 Introduction to Object Oriented Programming Chapter 10.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
1 More About Derived Classes and Inheritance Chapter 9.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Procedural and Object-Oriented Programming
OOP: Encapsulation &Abstraction
Polymorphism.
Review: Two Programming Paradigms
C++ Classes & Object Oriented Programming
Chapter 5 Classes.
Week 6 Object-Oriented Programming (2): Polymorphism
More Object-Oriented Programming
Polymorphism Polymorphism
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
CISC/CMPE320 - Prof. McLeod
Overview of C++ Polymorphism
C++ Programming CLASS This pointer Static Class Friend Class
Object-Oriented PHP (1)
CPS120: Introduction to Computer Science
Lecture 8 Object Oriented Programming (OOP)
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

1 C++ Classes & Object Oriented Programming Overview & Terminology

2 Object Oriented Programming Programmer thinks about and defines the attributes and behavior of objects. Often the objects are modeled after real- world entities. Very different approach than function-based programming (like C).

3 Reasons for OOP Abstraction Encapsulation Information hiding Inheritance Polymorphism Software Engineering Issues

4 Class: Object Types A C++ class is an object type. When you create the definition of a class you are defining the attributes and behavior of a new type. Attributes are data members. Behavior is defined by methods.

5 Creating an object Defining a class does not result in creation of an object. Declaring a variable of a class type creates an object. You can have many variables of the same type (class). Instantiation

6 Information Hiding The interface to a class is the list of public data members and methods. The interface defines the behavior of the class to the outside world (to other classes and functions that may access variables of your class type). The implementation of your class doesn't matter outside the class – only the interface.

7 Information Hiding (cont.) You can change the implementation and nobody cares! (as long as the interface is the same). You can use other peoples classes without fear!

8 Inheritance It is possible to extend existing classes without seeing them. Add whatever new behavior you want. Example: –You have a class that represents a "student". –Create a new class that is a "good student". Most of the behavior and attributes are the same, but a few are different – specialized.

9 Polymorphism The ability of different objects to respond to the same message in different ways. Tell an int to print itself: cout << i; Now tell a double: cout << x; Now tell the Poly: cout << poly;

10 Private vs. Public Public data members and methods can be accessed outside the class directly. The public stuff is the interface. Private members and methods are for internal use only. We will also see Protected.

11 Special Member Functions Constructors: called when a new object is created (instantiated). –can be many constructors, each can take different arguments. Destructor: called when an object is eliminated –only one, has no arguments.

12 Accessing Data Members Data members are available within each method (as if they were local variables). Public data members can be accessed by other functions using the member access operator "." (just like struct).

13 Accessing class methods Within other class methods, a method can be called just like a function. Outside the class, public methods can be called only when referencing an object of the class.

14 What happens here? class foo { int i;// # elements in the array int a[10];// array 10 at most // sum the elements int sum(void) { int i, x=0; for (i=0;i<i;i++) x+=a[i]; return(x); }... which i is it ?

15 Class Scope Operator :: You can solve the previous problem using the "::" operator classname::membername. for (i=0;i<foo::i;i++) x+=a[i];

16 Method names and :: Sometimes we put just a prototype for a member function within the class definition. The actual definition of the method is outside the class. You have to use :: to do this.

17 Example class foo {... int sum(void); }; int foo::sum(void) { int sum=0; for (int i=0;i<foo::i;i++) { sum += a[i]; } return(sum); }

18 Classes and Files The relationship between C++ class definitions and files depends on the compiler. In general you can put class definitions anywhere! Visual C++ wants one class per file. Most people do this: –class definition is in classname.h –any methods defined outside of the class definition are in classname.cpp

19 static methods A static method is a class method that can be called without having an object. Must use the :: operator to identify the method. Method can't access non-static data members! (they don't exist unless we have an object). See the file "staticmem.cpp" for a complete example

20 Static Data Members It is possible to have a single variable that is shared by all instances of a class (all the objects). –declare the variable as static. Data members that are static must be declared and initialize outside of the class. –at global or file scope.

21 Static data member example class foo { private: static int cnt; public: foo() { foocount++; cout << "there are now " << cnt << " foo objects << endl; }... See the file "staticmem.cpp" for a complete example

22 A Problem Class definitions usually in a ".h" file. Can be included by many other ".cpp" files, all part of the same program. Can't declare and initialize a static data member in the ".h" file.

23 Solution class foo{ static int cnt; … }; int foo::cnt=1; foo.h foo.cpp #include "foo.h"... #include "foo.h"... int main() {... f1.cpp f2.cpp

24 Friends A Class can declare other classes as "friend classes". A Class can declare external functions as "friends". friends can access private members and methods.

25 Friend Declaration class foo { private: int i,j; … friend class fee; friend int printfoo( foo &f1); };

26 Operator Overloading We can define what some C++ operators mean when applied to user defined objects. vector example ( ivec.h ) defines operators: ostream << istream >> + - [] = ==

27 Operator Overloading Restrictions You can't: –define a completely new operator –redefine the meaning of operators on built-in data types (like int or double ). –Change operator precedence or associativity.

28 Inheritance You can create a new class that inherits from an existing class. You can add new members and methods. You can replace methods. The new class is a specialization of the existing class.

29 Terminology The existing class is called the base class. The new class is called the derived class. Objects of the derived class are also objects of the base class.

30 Inheritance Example Base class is shape, represents the abstract notion of a shape. Derived classes: –rectangle –circle –triangle. An object that is a circle is also a shape!

31 Inheritence Example Code Complete example on the WWW deals with students, compsci students and biology students. –student has name and average –compsci has name, average and list of computer languages. –bio has name, average and list of organisms.

32 Protected Class members/methods We've already seen private and public. Protected means derived classes have access to data members and methods, but otherwise the members/methods are private.

33 Public/Private/Protected Inheritance Usually use public inheritance. Private and Protected inheritance change what members/methods can be accessed outside of the class. Complicated rules - see the table on page 535 for the gory details.

34 Polymorphism You can treat derived class objects as objects of the base class. Example studentlist on the WWW does this. The problem is that when doing this we don't get access to the derived class methods!

35 Virtual Functions A virtual function is a method in a base class that can be overridden by a derived class method. In the student list example, we would like each student object to "know" which print() method to use. Declare the base class print method virtual, and this happens!