Object-Oriented Programming in C++

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
I NHERITANCE Chapter 10. I NHERITANCE Mechanism for enhancing existing classes You need to implement a new class You have an existing class that represents.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Object-Oriented Programming in C++ Lecture 6 Inheritance.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Object-Oriented Programming in C++
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Writing Classes (Chapter 4)
Module 7: Object-Oriented Programming in Visual Basic .NET
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Java Classes Using Java Classes Introduction to UML.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CE Further Programming Concepts in C++ Lecture 5 Inheritance & Polymorphism.
Classes CS 21a: Introduction to Computing I First Semester,
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Defining New Types Lecture 21 Hartmut Kaiser
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Object-Oriented Design Simple Program Design Third Edition A Step-by-Step Approach 11.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Object-Oriented Programming in C++ More examples of Association.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Further Programming Concepts in C++ Ce Lecture 1 About the module Lectures plus Q&A sessions Tutorials The Assessment Assignment Class Test (Multi.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
1 Strings, Classes, and Working With Class Interfaces CMPSC 122 Penn State University Prepared by Doug Hogan.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Chapter 3 Implementing Classes
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Lecture 3 John Woodward.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Introduction to Classes
Chapter 5 Classes.
Creating and Using Classes
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Data Abstraction
Classes, Objects, Methods and Strings
Classes CS 21a: Introduction to Computing I
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Object-Oriented Programming in C++ Lecture 2 Classes and Objects

Introduction Last lecture we introduced the module introduced C++ This lecture we will review the concept of classes and objects discuss their representation using UML class diagrams implement and use a simple C++ class introduce C++ structures and enumeration types

Classes and objects a class is a programmer-created data type it can have member variables (attributes) to hold data and member functions (methods) to manipulate that data an object is an instance of a class with specific values for the member variables normally the member variables are private hidden within the object we interact with the object via its public methods encapsulation

Example – Account class consider a class to represent a bank account what data do we need to store about a bank account? account balance interest rate what functions are needed to manipulate this data? get the balance, deposit money, withdraw money, calculate and add the interest create an account with a given initial balance and interest rate each bank account object will have its own balance and interest rate in this example

UML class diagrams UML (the Unified Modelling language) defines 13 standard diagrams for system modelling the class diagram is a useful way to represent a class its member variables and methods their type and visibility UML diagrams can have different levels of detail from initial ideas (analysis) to implementation-level documentation

UML diagram of an Account balance interest rate create account get balance deposit money withdraw money add interest the diagram has 3 sections the class name is at the top the variables are in the middle the functions are at the bottom this is an analysis-level diagram what should the implementation-level diagram look like?

Implementation-level diagram of an Account -balance: double -interestRate: double +Account(initalBalance: double, rate: double) +getBalance() : double +deposit(amount: double) +withdraw(amount: double) +addInterest() the member visibility is shown by: private + public # protected the type of each member is shown after the member name

C++ implementation of the Account class class Account { private: // optional because members are private by default double balance; // amount of money held by this account double interestRate; // a monthly or yearly interest rate public: // create an account with an initial amount and a specified interest rate Account(double initialBalance, double rate) : balance(initialBalance), interestRate(rate){ } // return the account's balance double getBalance(){ return balance;} // add money to the account void deposit(double amount) { balance += amount; } // continued on next slide

C++ implementation of the Account class void withdraw(double amount) { // implement this method yourself } // add money according to the interest rate. void addInterest() { balance *= (1 + interestRate/100.0); // this is the same as balance = balance * (1 + (interestRate/100) ); };

Using the Account class create a project put the Account class code into a header file called account.h this is the account class definition create a source file called bank.cpp this will contain the main method which will create and test an Account object

bank.cpp #include <iostream> #include "account.h" using namespace std; void main(void) { Account stdAccount(100, 4); // create an account with £100and 4% interest rate stdAccount.addInterest(); cout << "Balance: " << stdAccount.getBalance()<< endl; stdAccount.deposit(50); stdAccount.withdraw(100); }

#include directives #include <iostream> #include "account.h" iostream is a library file the angled brackets indicate the file is in the C++ library directory path should already been set up in IDE account.h was written by the programmer the quotes indicate the file path and name in this case, it is in the same directory as bank.cpp

Constructors the constructor is called when an object is first created sets up the object in memory with space for the member variables if the class does not have a constructor, a default no-argument constructor is used a constructor must have the same name as the class and no return value

Account constructor the constructor initialises the member variables balance and interestRate Account(double initialBalance, double rate) : balance(initialBalance),interestRate(rate) { } this is equivalent to the Java-like constructor Account(double initialBalance, double rate) { balance = initialBalance; interestRate = rate; } the first version initialises the member variables as they are created the second creates the member variables then assigns them values within the constructor body less efficient

Destructors a destructor is called when the object is destroyed goes out of scope or is deleted a destructor has the same name as the class, preceded by a ~ it cannot have any parameters or return value it is called automatically – never call it yourself a destructor for the Account class: ~Account() { cout << "Closing the account. Final balance: " << balance << endl; }

C++ structures C++ structures are similar to classes no encapsulation they can have member variables and functions however all members are public by default no encapsulation an Account struct could have its balance changed directly rather through the deposit and withdraw methods structs are useful to group together related data

Example structures struct Point3D { float x; float y; float z; }; int main() { Point3D A = { 10, 5.3, 6}; Point3D B; B.x = 2.1; B.y = 2.4; B.z = 7.3; //.... }

Enumeration types another useful programmer-defined type is the enumeration defines all possible values of a given type enum colour { RED,ORANGE,YELLOW,GREEN,BLUE,VIOLET }; int main() { colour hatColour = ORANGE; colour shirtColour = VIOLET; // .... } each enum value maps to an integer RED is 0, ORANGE is 1…. less error-prone than using strings or constants

Summary In this lecture we have In the tutorial we will reviewed the concept of classes discussed their representation using UML class diagrams implemented and used a simple C++ class introduced C++ structures and enumeration types In the tutorial we will implement the Account class add more functionality and a menu-driven interface

Further reading Object Management Group – UML Resource Page http://www.uml.org/ MSDN library – C++ language reference Classes, structures and unions http://msdn.microsoft.com/en-us/library/4a1hcx0y.aspx