Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.

Slides:



Advertisements
Similar presentations
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Advertisements

Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Structuring Your Data Using Classes Chapter 8. What We’ll Cover in Chapter 8 Classes and how they are used The basic components of a class How a class.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Defining New Types Lecture 21 Hartmut Kaiser
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
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.
Learners Support Publications Classes and Objects.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Inheritance One of the most powerful features of C++
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 10 Introduction to Classes
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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?
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Chapter 2 Objects and Classes
Procedural and Object-Oriented Programming
Classes (Part 1) Lecture 3
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?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
7. Inheritance and Polymorphism
Review: Two Programming Paradigms
Introduction to Classes
Chapter 5 Classes.
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Object-Oriented Programming Using C++
Chapter 2 Objects and Classes
Introduction to Classes
Classes and Objects.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors –Default constructors –Copy constructor Friend class/function The ‘this’ pointer

Object-Oriented Programming Encapsulation Data hiding –Data Member  state, feature –Member function  interface, method Inheritance –Base class –Derived class Polymorphism

Encapsulation

Data Hiding

Inheritance

Polymorphism

Terminology A class is a user-defined data type Variables of a class are called data members, and the functions are called member functions which is referred to methods One declares variables of the class type also called instances of the class. Each instance is an object of the class.

Terminology Defining an instance of a class is sometimes referred to as instantiation Object-oriented programming involves the ideas of encapsulation of data, polymorphism

Defining a Class Access specifier –private –public –protected class and struct Program 12.1

Constructors Memory space generating Program 12.2 Rewrite program 12.2a –box.h, box.cpp, use_box.cpp

Default Constructor If you don’t define a constructor for your class, then the compiler supplies a default constructor that is used to create objects of your class int main() { Box smallBox(80.0, ); Box mediaBox; // compiler error, constructor already specified … } Program 12.3

Default Value class Box { … Box(); Box(double length = 1.0, double breadth = 1.0, double height = 1.0); }; int main() { Box smallBox; // Compiler error, ambiguity … }

Default Initialization Values Using initializer list // Constructor Definition Box::Box(double la, double lb, double h): length(la), breadth(lb), height(h) { cout << “Box Constructor called” << endl; }

The explicit Keyword Prevent automatically data type cast Program 12.4a

Private Members of a Class Proper initialized list Prevent improper modification Box theBox(10.0, 10.0, 5.0); theBox.length = -20.0; Private members are not accessible Program 12.4

Private Members

Accessing Private Class Members The private members can be accessed by public member functions, accessor Program 12.5a

The Default Copy Constructor If you don’t define copy constructor, the compiler supplies a default constructor that allows an object to be created Copy constructor: create a new instance by copying the exist one Program 12.5

Friends Friend classes – a whole class can be specified as a friend of a class Friend functions – an individual function can be specified as a friend of a class

The Friend Functions of a Class A function that is not a member of a class but nonetheless can access all its members is called a friend function Friend function is declared in one class Friend function can be declare as a global function or a member of a class Program 12.6

The Pointer Named ‘this’ // usually ‘this’ pointer is hidden double Box::volume() {return length * breadth * height; } // Same as double Box::volume() { return this->length * this->breadth * this->height; } Program 12.7

const Objects and const Member Functions class Box { int compareVolume(const Box& otherBox) const; } // in main() Box theBox(17.0, 11.0, 4.4), otherBox(8.0, ); if (theBox.compareVolume(otherBox)) {… } Program 12.8a

Mutable Data Members of a Class If you declare an object as const, you can’t change the values of the data members of the object because they’ll also be effectively const. However, you may find situations in which you need to allow certain, selected data members of a class to be altered, even if the object was declared as const How? –By declaring a data member as mutable

Mutable Data Members of a Class class SecureAccess { public: … bool isLocked() const; private: … mutable int time; }; bool SecureAccess::isLocked() const // definition {time = getCurrentTime(); return lockStatus(); } const SecureAccess mainDoor; // in main() bool doorState = mainDoor.isLocked(); // calling

Casting Away const Very rare situations to use it A function is dealing with a const object, either passed as an argument of the object pointed to by this, and it is necessary to make it non-const Make const type to non-const, the expression must be a const Type const_cast (expression)

Arrays of Objects of a Class Construct every element in an object array by calling default constructor Program 12.8

Boundary Alignment For the reasons of efficiency, a 2-byte and a 4-byte variables must be placed at the addresses that is a multiple of two, and four respectively. The size needed for variable is larger than it truly used Program 12.9

Static Data Members of a Class When you declare a data member of a class as static, the effect is that the static data memeber is defined only once, and will exist even if no objects of the class have been created

Defining a static Member class Box { pirvate: static int objectCount; double length; double breadth; double height; }; // should be define in global region int Box::objectCount = 0; Program 12.10

Access Static Data Members Access the static variable in a class Program 12.10a (in case doesn’t exist, the following two snaps can create 12.10a)

A Static Data Member of the Same Type As the Class

A static member function is a full member of the class in terms of access privileges