Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Our BankAccount class should define three methods  deposit  withdraw  getBalance How a programmer will carry out these operations?  We assume that.
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Working with Classes. A method dealing with two objects C++ has a pointer which call this. It gives the address of an object. Suppose we have two objects.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Object-oriented analysis (OOA) techniques are used to (1) study existing objects to see if they can be reused or adapted for new uses, and (2) define new.
Chapter 3  Implementing Classes 1 Chapter 3 Implementing Classes.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
C++ fundamentals.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Writing Classes (Chapter 4)
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Three: Implementing Classes.
Object-Oriented Programming in C++
Classes CS 21a: Introduction to Computing I First Semester,
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Section 6 - Object-Oriented C++ - Classes. The idea of OO programming is to model the features (aka properties or attributes) and behaviour of real-world.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Introduction to Classes, Objects, Methods and strings
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Fall 2006Slides adapted from Java Concepts companion slides1 Implementing Classes Advanced Programming ICOM 4015 Lecture 3 Reading: Java Concepts Chapter.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
INFSY 535.  Small systems  Larger systems 1.Understand the program requirement- what 3. Write and test each part (unit testing) 4. Maintenance 2. Specify.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Chapter 3: Implementing Classes Part 1. To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Lecture 3 John Woodward.
Chapter 3 – Implementing Classes
Chapter Three: Implementing Classes
Implementing Classes Yonglei Tao.
Chapter 4: Writing Classes
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
Chapter 3 Implementing Classes
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
Defining Classes and Methods
Chapter 9 Introduction To Classes
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Function Overloading Having more than one function with the same name. list of argument of a function are called signature of a function. We can have more than one function with the same name but they must have different signatures.

void print ( char * str, int length); void print( double d, int length); void print( long l, int d); void print (long v, int length);

Function Template A function template is a generic function description. We define a function as a generic type and we use it as a specific type ( double or int,…) later on.

Example template // template void swap ( Any &a, Any &b) { Any temp; temp=a; a=b; b=temp; }

#include template //template void swap ( Any &a, Any &b); int main() { int i=10; int j=20; cout<<“i, j=“<<i<<“ “<<“, “<<j<<“. \n”; swap(i,j); cout<<“Now i, j=“<<i<<“ “<<“, “<<j<<“. \n”; double x=24.5; double y=81.7;

cout<<“x, y=“<<x<<“ “<<“, “<<y<<“. \n”; swap(x,y); cout<<“Now x, y=“<<x<<“ “<<“, “<<y<<“. \n”; return 0; } template void swap ( Any &a, Any &b) { Any temp; temp=a; a=b; b=temp; }

Overloaded Template #include template //template void swap ( Any &a, Any &b); // original template template void swap(Any *a, Any *b, int n); template void show(Any a[ ]);

const int lim=8; int main() { int i=10; int j=20; cout<<“i, j=“<<i<<“ “<<“, “<<j<<“. \n”; swap(i,j); cout<<“Now i, j=“<<i<<“ “<<“, “<<j<<“. \n”; int d1[lim]={0,7,0,4,1,7,7,6}; int d2[lim]={0,6,2,0,1,9,6,9};

show(d1); show(d2); swap(d1,d2,lim); return 0; } template void swap ( Any &a, Any &b) { Any temp; temp=a; a=b; b=temp; }

template void swap ( Any []a, Any []b, int n) { Any temp; for( int i=0; i< n; i++) { temp=a[i]; a[i]=b[i]; b[i]=temp; }

template void show(Any a[]) { for(i=0;i<lim;i++) cout<<a[i]; cout<<endl; }

Introduction to OOP Black Box : A black box magically does its thing Hides its inner workings Encapsulation: the hiding of unimportant details Abstraction: taking away inessential features, until only the essence of the concept remains

In object-oriented programming the black boxes from which a program is manufactured are called objects

Levels of abstraction: A Real Life Example Black boxes in a car: transmission, electronic control module, etc.

Levels of abstraction: A Real Life Example Users of a car do not need to understand how black boxes work Interaction of a black box with outside world is well-defined –Drivers interact with car using pedals, buttons, etc. –Mechanic can test that engine control module sends the right firing signals to the spark plugs –For engine control module manufacturers, transistors and capacitors are black boxes magically produced by an electronics component manufacturer

Encapsulation leads to efficiency: –Mechanic deals only with car components (e.g. electronic control module), not with sensors and transistors –Driver worries only about interaction with car (e.g. putting gas in the tank), not about motor or electronic control module

Objects and Classes Object: entity that you can manipulate in your programs (by calling methods) Each object belongs to a class. For example, cout belongs to the class iostream

Method (Function) Method : Sequence of instructions that accesses the data of an object You manipulate objects by calling its methods Class: Set of objects with the same behavior

Levels of abstraction: Software Design

Old times: computer programs manipulated primitive types such as numbers and characters Manipulating too many of these primitive quantities is too much for programmers and leads to errors Solution: Encapsulate routine computations to software black boxes Abstraction used to invent higher-level data types

In object-oriented programming, objects are black boxes Encapsulation: Programmer using an object knows about its behavior, but not about its internal structure In software design, you can design good and bad abstractions with equal facility; understanding what makes good design is an important part of the education of a software engineer First, define behavior of a class; then, implement it

Designing the Public Interface of a Class class ClassName { private : instance fields; // we can have methods as well public : constructors; // header definition methods; // header definition destructors; // header definition };

Designing the Public Interface of a Class Behavior of bank account (abstraction): deposit money withdraw money get balance

Designing the Public Interface of a Class: Methods Methods of BankAccount class: deposit withdraw getBalance We want to support method calls such as the following: harrysChecking.deposit(2000); harrysChecking.withdraw(500); cout<<harrysChecking.getBalance();

Instance Fields An object stores its data in instance fields Field: a technical term for a storage location inside a block of memory Instance of a class: an object of the class The class declaration specifies the instance fields: class BankAccount { private : double balance; string name; }

Instance Fields An instance field declaration consists of the following parts: –specifying access (such as private) –type of variable (such as double) –name of variable (such as balance) Each object of a class has its own set of instance fields You should declare all instance fields as private

Syntax : Instance Field Declaration class ClassName { accessSpecifier: fieldType fieldName;... }; Example: class BankAccount { private: double balance;... } Purpose: To define a field that is present in every object of a class

Syntax : Method Definition returnType ClassName :: methodName(parameterType parameterName,...) { method body } Example: void BankAccount :: deposit(double amount) {... } Purpose: To define the behavior of a method

Accessing Instance Fields The deposit method of the BankAccount class can access the private instance field: void BankAccount :: deposit(double amount) { double newBalance = balance + amount; balance = newBalance; } Other methods cannot: int main() { BankAccount momsSavings = new BankAccount(1000);... momsSavings.balance = -1000; // ERROR }

Encapsulation = Hiding data and providing access through methods

Self Check How can you use the methods of the public interface to empty the harrysChecking bank account? Suppose you want a more powerful bank account abstraction that keeps track of an account number in addition to the balance. How would you change the public interface to accommodate this enhancement?

Answer harrysChecking.withdraw(harrysCheckin g.getBalance()) Add an accountNumber parameter to the constructors, and add a getAccountNumber method. There is no need for a setAccountNumber method– the account number never changes after construction.

Implementing Constructors Constructors contain instructions to initialize the instance fields of an object BankAccount :: BankAccount() { balance = 0; } BankAccount :: BankAccount(double initialBalance, string str) { balance = initialBalance; name=str; }

Constructor Call Example BankAccount harrysChecking = new BankAccount(1000,”Dave”); –Create a new object of type BankAccount –Call the second constructor (since a construction parameter is supplied) –Set the parameter variable initialBalance to 1000 –Set the balance instance field of the newly created object to initialBalance –Return an object reference, that is, the memory location of the object, as the value of the new expression –Store that object reference in the harrysChecking variable

Class Destructor class ClassName { …….. public : ~className(); ~className(int ); …….. };

Implementing Destructores BankAccount :: ~BankAccount() { cout<<‘good bye”; }

Testing a Class Test class: a class with a main method that contains statements to test another class. Typically carries out the following steps: Construct one or more objects of the class that is being tested Invoke one or more methods Print out one or more results

#include // class definition ………. ……….. int main() { BankAccount harrysChecking = new BankAccount(); harrysChecking.deposit(2000); harrysChecking.withdraw(500); cout<<(harrysChecking.getBalance()); }

Each class in separate file We use a header file “Name.h” to use a class later or separate the classes. Each header file (.h) declare a class. We use a.cpp file to implement the body of the methods.

Header file for BankAccount class /////////// bank1.h #ifndef BANK1_H #define BANK1_H class BankAccount { private : double balance; string name; public : BankAccount(); BankAccount(double initial_balance); void deposit (double amount); void withdraw (double amount ); double getBalance(); ~BankAccount(); }; #endif

CPP file #include #include “bank1.h” BankAccount :: BankAccount() { balance = 0; } BankAccount :: BankAccount(double initialBalance, string str) { balance = initialBalance; name=str; }

void BankAccount ::deposit(double amount { balance =balance+amount ; } void BankAccount::withdraw(double amount) { if ( amount > balance ) amount=0; else balance=balance-amount; }

double BankAccount :: getBalance() { return balance; } BankAccount :: ~BankAccount() { cout<<‘good bye”; }

#include #include “bank1.h” int main() { BankAccount harrysChecking; harrysChecking.deposit(2000); harrysChecking.withdraw(500); cout<<(harrysChecking.getBalance()); return 0; }

Array of objects BankAccount Bank[4]={ BankAccount(12.25, “Dave”), BankAccount(100.00, “Sue”), BankAccount(), BankAccount(200.00, “Ali”) };