CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Chapter 6: A First Look at classes. Object-Oriented Programming  Object-oriented programming is centered on creating objects rather than procedures.
Classes and Objects Systems Programming.
Starting Out with C++: Early Objects 5th Edition
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 6: A First Look at Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 10 Introduction to Classes
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Chapter 10 Classes and Objects In-Depth. Chapter 10 A class provides the foundation for creating specific objects, each of which shares the general attributes,
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
CS16: UML’s Unified Modeling Language. UML Class Diagram A UML class diagram is a graphical tool that can aid in the design of a class. The diagram has.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 6 A First Look at Classes. 2 Contents 1. Classes and objects 2. Instance Fields and Methods 3. Constructors 4. Overloading Methods and Constructors.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
classes and objects review
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Introduction to Classes
Classes and Data Abstraction
Classes and Objects.
Lecture 5- Classes, Objects and Methods
Lecture 8 Object Oriented Programming (OOP)
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

CSci 162 Lecture 10 Martin van Bommel

Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program –Send data to the procedures, get results back –Data and functions separate Object-Oriented Programming –Centered around the object (abstract data types that encapsulate data and procedures together) –Send messages to objects to work with them –Only object’s member functions can operate on data

Object-Oriented Terminology Object –software entity that contains both data and procedures (encapsulation) Member Attributes –The data items contained in (describe) the object Member functions –Procedures that an object performs

Data Hiding Object’s internal data is hidden (cannot be accessed directly) from outside code Access to the data is restricted to the object’s member functions Changes can be made to internal structure as long as interface does not change To drive a car, do not need to know internals

Classes and Objects Class –Describes the attributes and member functions of a specific type of object –e.g. Blueprint of a house Object –An instance of a particular class –e.g. A particular house built using the blueprint –Many houses can be built from same blueprint

Defining a Class class ClassName { private: // access specifier // declarations of member variables // and private member functions public: // access specifier // declarations of public member // functions (prototypes) };

Initial Class - Rectangle class Rectangle { private: double width; double length; public: void setWidth (double); void setLength (double); double getWidth ()const; double getLength ()const; double getArea ()const; };

Notes on Rectangle Class Public member functions are provided for access to the private data members –get and set are typically used –Area will be calculated from width and length Key word “const” used to specify that function will not change data members –Catches unintentional data update

Implement Member Functions void Rectangle::setWidth(double w) { // mutator width = w; // (setter) } double Rectangle::getLength() const { // accessor return length; // (getter) } double Rectangle::getArea() const { return width * length; } Note: :: is called scope resolution operator

Object Definition A class is a template of an object To create an object that is an instance of a class, use: –ClassName objectName; Defining an object (declaration of a variable) is called the instantiation of a class –e.g. Rectangle box;

Calling Member Functions To call on an objects member functions, use the dot operator –E.g. box.setWidth(5.25); Can use the notation as part of an expression if a value is returned –E.g. a = box.getArea();

Inline Member Functions When the body of a member function appears inside a class declaration, it is declared inline. Inline functions are compiled differently –Compiler replaces call to inline function with the code of the function itself –Thus it removes the overhead for the call itself –Increases size of the executable file if a large inline member function is called several times

Constructors A constructor is a member function with the same name as the class, but with no specified return type, and is automatically called when an object is created in memory, or instantiated. Rectangle::Rectangle() { } The default constructor takes no arguments If none declared, one created automatically

Constructor Arguments A constructor can have parameters public: Rectangle(double w, double l) { width = w; length = l; } Arguments passed when object is created Rectangle box(5.0, 6.0);

Default Arguments Functions, including constructors, can have default arguments Passed to function if no argument provided Default value listed in function header public: Rectangle(double w = 0.0, double l = 0.0) { width = w; length = l; }

Default Constructor A constructor with no arguments is the default constructor A constructor with default arguments for all of its parameters, which can be called with no arguments, is the default constructor May have only one default constructor If constructor defined but it cannot become default constructor, no default created

Destructor A destructor is a member function that is automatically called when an object is destroyed (cannot call a destructor) Destructor name is the name of the class preceded by a tilde (~) (with no parameters) Can add actions to a destructor ~Rectangle() { cout << ”Destroy!”; }

Private Member Functions Private member functions may only be called from member functions of the same class Necessary for the internal function of another member function, but not called from outside Included in the “private” declarations Defined either inline or in an implementation file