A Computer Science Tapestry 12.1 Tools for programming l Syntatic details of control and variables  If, while, switch, function calls  Scope and lifetime.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Programming Languages and Paradigms
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
OOP Languages: Java vs C++
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract 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.
Pointer Data Type and Pointer Variables
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Inheritance Inheritance – most important and a useful feature of OOPs supported by C++ | Website for Students | VTU -NOTES -Question Papers.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
The Java Programming Language
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Pointers OVERVIEW.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
A Computer Science Tapestry 12.1 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3,
Written by: Dr. JJ Shepherd
Overview of C++ Polymorphism
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
CPS What's a pointer, why good, why bad? l Pointer is a memory address, it's an indirect reference to memory or an object.  Rather than say we.
OOP Basics Classes & Methods (c) IDMS/SQL News
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
CPS What is Computer Science? What is it that distinguishes it from the separate subjects with which it is related? What is the linking thread.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Copyright © Jim Fawcett Spring 2017
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS3340 – OOP and C++ L. Grewe.
Review: Two Programming Paradigms
What's a pointer, why good, why bad?
Programming Language Concepts (CIS 635)
Object-Oriented Programming Using C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
More Object-Oriented Programming
Focus of the Course Object-Oriented Software Development
Object-Oriented Programming Using C++
Overview of C++ Polymorphism
Pointers, Memory, Abstractions
C++ Object Oriented 1.
Presentation transcript:

A Computer Science Tapestry 12.1 Tools for programming l Syntatic details of control and variables  If, while, switch, function calls  Scope and lifetime of variables l Arrays, strings, other containers (e.g., StringSet)  Hold homogenous collection, support certain operations  String, e.g., is collection of char values l Object oriented and object based programming  Encapsulation of state and behavior in classes  Data and the operations on it belong together  Object orientation requires inheritance/polymorphism

A Computer Science Tapestry 12.2 Toward understanding inheritance l Consider original/old pixmap code for interacting with user  Several kinds of operations: load, store, invert, reflect, …  Each operation requires code spread over pixchoices.cpp Edit working program, add code to code that already works Not haphazard, but dangerous: if it ain’t broke, don’t touch Initialization, selection of operation, implementation, … l The open-closed principle of program building/design  Programs/classes should be open for extension, but closed to modification (Bertrand Meyer and Robert Martin)  How do we change code without altering it? This is a goal, in practice we modify some, but minimize l Inheritance lets us realize the principle

A Computer Science Tapestry 12.3 Use interfaces, not implementations l See PixOperator in pixoperator.h, used in pixchoices.cpp  Each subclass must implement operate method Pure virtual in parent class, requires subclass implementation Which virtual function determined at runtime l We have parent, base, or super -class for interface  Child or Subclasses extend the parent class ( implement interface)  pointers to parent class, which can point to child object I’m pointing at an animal (could be marsupial or bird) l Child/sub classes don’t have access to private data/functions, but the data are there (alternative: use protected)

A Computer Science Tapestry 12.4 Syntactic details l Functions in parent class that are changeable in subclasses must be declared as virtual  The “real” function is determined at runtime  In parent/base/super class make all functions virtual  We must have a virtual destructor that’s implemented l For interface only functions, that have no default version  Pure virtual functions, aka abstract functions  Syntax requires = 0 in.h file  Can’t instantiate an object of an abstract (base) class Doesn’t matter, really, we use pointers to objects

A Computer Science Tapestry 12.5 Conceptual Details l Some behavior is implemented in parent class  Can be changed or overridden in subclasses, but we won’t do this in pixmap program l Some behavior specified in parent class, implemented in subs  Pure virtual functions in parent class make it abstract l In C++ inheritance requires pointers or references  No copies of objects passed around  Pointers required for storing in vectors, maps, etc. l Runtime determination of behavior, runtime allocation of storage

A Computer Science Tapestry 12.6 What inheritance gets us (old/new) static string Operations [] = { "(load) an image ", "(save) an image ", … }; void PixChoices::operate(string op) { string s = LowerString(op); if (s == "load"){ doLoad(); } else if (s == "save") { doSave(); } … } void PixChoices::doLoad() { // code here } void PixChoices::doSave() {…} PixChoices::PixChoices() { PixOperator * temp = 0; temp = new PixLoader(); temp = new PixSave(); … } void PixChoices::operate(string op) { string s = LowerString(op); PixOperator * op = myOps[s]; if (op == NULL){ cout << “no operation for " << s << endl; } else { op->operate(myPix); }

A Computer Science Tapestry 12.7 Object based v. Object oriented l Original pixmap code in pixchoices.cpp  All code in one file, advantages and disadvantages  Every pixmap operation in at least three places  What happens in “big” programs? What about testing? l New version has all code for operations in one place: subclass  Easy to develop new operations: where does code go?  Small modification to pixchoices.cpp required, where? l Hard to see relationships  Original could read one file  New version many files, maps, …

A Computer Science Tapestry 12.8 Pointers, Memory, Abstractions l A pointer is the a variable/value that is a memory address  Addresses like 1, 2, 3, …, 0x0024ab03 Hexadecimal or base-16 digit represents 4 bits Character is 8 bits, integer is 32 bits  Every variable is stored somewhere in memory, typically we can ignore where double x = 32.6; int y = 18; string s = "hello";  The string variable s is actually the same size as int y Storage for the letters is elsewhere, string references it l What about a permanent, but forwardable address? "hello" 0x00 0x08 0x0c 0x??

A Computer Science Tapestry 12.9 What's a pointer, why good, why bad? l Pointer is a memory address, it's an indirect reference to memory or an object.  Rather than say we have an int, we say we have a pointer to an int  If x is an int, xptr can be a pointer to x Same thing works with Date, Dice, Student, … Not much use to have pointer to int, but pointer to class!! l Pointers force us to think about the machine and memory  Knowledge is powerful, freedom from it liberating l Pointers allow us to work at a lower level, but permit inheritance and a higher level of design/programming  Built-in array and tvector, C-style string and

A Computer Science Tapestry Pointer basics and terminology l new, dereference, selector operator, copy semantics CD c1("Beatles", "Rubber Soul", 1965); CD c2("Nirvana", "Nevermind", 1991); CD * c3 = new CD("REM", "Reveal", 2001); CD * c4; // what is the value of c4? CD c5; // what is the value of c5? cout << c1.title() << endl; cout title() << endl; cout << (*c3).title() << endl; c5 = c2; c2.changeTitle("Incesticide"); cout << c5.title() << endl; c4 = c3; c3->changeTitle("Out of Time"); cout title() << endl; What happens if we print c4->title() on first line? Why?

A Computer Science Tapestry What's the point? l What's the difference between a vector of Dates and a vector of pointers to Dates? What about Courses, Students, etc.? tvector tv(1000); tvector tvp(1000);  Which takes up more space? What are values in vectors?  What happens when we write tv[0] = tv[2]; // if we change tv[2], affect tv[0]? tvp[3] = new Date(12,19,2003); tvp[0] = tvp[3]; // change *(tvp[3]), affect tvp[0], *tvp[0]? l Compile time objects compared to runtime objects  For pointers, objects created at runtime, not compile time!

A Computer Science Tapestry Who is Alan Perlis? l It is easier to write an incorrect program than to understand a correct one l Simplicity does not precede complexity, but follows it l If you have a procedure with ten parameters you probably missed some l If a listener nods his head when you're explaining your program, wake him up l Programming is an unnatural act l Won first Turing award