Classes and Objects April 6, 2011. Object Oriented Programming Creating functions helps to make code that we can reuse. When programs get large it becomes.

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.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
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.
ASP.NET Programming with C# and SQL Server First Edition
Structures 11/5/10. Reading  Read pp  Review of scope ch6/review.cpp ch6/review.cpp What is the output? What is the output?
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
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.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
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.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Why Use Classes - Anatomy of a Class.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Chapter More on Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
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.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Matthew Small Introduction to Object-Oriented Programming.
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.
Chapter 3 Implementing Classes
Class Definitions and Writing Methods Chapter 3 3/31/16 & 4/4/16.
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Review What is an object? What is a class?
Andy Wang Object Oriented Programming in C++ COP 3330
Review: Two Programming Paradigms
classes and objects review
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Chapter 5 Classes.
C++ Classes & Object Oriented Programming
Introduction to Classes
Object-Oriented Programming
Learning Objectives Classes Constructors Principles of OOP
Starting to think about objects...
Object-Oriented Programming
Object-Oriented Programming
Defining Classes and Methods
Object-Oriented PHP (1)
Lecture 8 Object Oriented Programming (OOP)
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Classes and Objects April 6, 2011

Object Oriented Programming Creating functions helps to make code that we can reuse. When programs get large it becomes difficult to manage so many functions. functions are organized into objects. Objects are more intuitive to use.

Objects in Programs You already know about the ifstream class Objects of this type can do the following functions: close(), fail(), clear() etc.

Encapsulation You use the functions from the ifstream, like close(), without knowing the details of the implementation. Easier not to have to understand how the code looks. This hiding is called encapsulation. When you create an object from scratch you should make it easy to use w.o. knowing the details of the methods.

Specify the Public Interface How the class interacts with a programmer is called the interface. The functions you can call. Example: I want a class to represent a rectangle. I want the interface to allow the programmer to find the area. I also want him to be able to find the perimeter. I will also add some functions to allow the user to set the length and width to desired values.

Interface Design Rectangle public: setLength(double l); setWidth(double w); findArea( ); findPerimeter( );

Member Variables In order to find the rectangle’s area and perimeter, the rectangle will need to know its length and width. Outside users don’t need to know the information. It is private.

Instance Variables Added Class Design Rectangle public setLength(double l); setWidth(double w); findArea( ) findPerimeter( ) private double length double width

Implementing the class. Now that I have a design, I can implement the class. rectangle.cpp Need to put private instance variables in the class.

Implementing the class. Rectangle Finish the functions, using the data members to compute the measurements. Once I finish writing the class, I need to create an object so that I can test the class. I’ll write a main method to create a rectangle or two.

Class Definition Syntax class Sample { public: fnc_prototype(); fnc_prototype2(); //... etc. private: member_var1; member_var2; // … etc. };

Member Function Definition return_type class_name :: function_name(parmlist)‏ { statements } :: is scope resolution operator

Calling a function Object_name.function_name(arg list); e.g. High.input_T( );

Scope of data members Can use data members in any of the functions in the class. There is no need to pass them to functions.

Constructor Has the same name as the class. Is executed automatically when a new object is made. Can be passed some values to initialize the instance variables. Now, I am ready to instantiate or create an object

Another Example – a Car Class Design and Implement a Car class. A Car should be able to tell you how far it can go on the gasoline left in the tank. It should also be able to go “Vroom!”. What will the interface look like? What data members will it have? How will its constructor look? What should a main() method to test it do?

Another Class

Sphere class Design and implement a class called Sphere that contains member data that represents the sphere’s diameter. Define a Sphere constructor to accept and initialize the diameter. Include functions to return the volume and the surface area of the Sphere. Write a main function to create and test a Sphere.

Accessor Function A function that allows the main() function to see a private data member. Usually starts with “get” Return the private data. I’ll add a getDiameter() to the Sphere class.