Class Circle { Float xc, yc, radius;

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
CH 8 : Enhancing Classes - Review QUICK REVIEW : A Class defines an entity’s (Object’s) data and the actions or behaviors (Methods) associated with that.
18. DECLARATIONS.
Learners Support Publications Classes and Objects.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 31 Discussion, Week 8 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:00-1:00pm.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Structures, Classes and Objects Handling data and objects Unit - 03.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
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.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
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?
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Eine By: Avinash Reddy 09/29/2016.
Topic: Classes and Objects
Learning Objectives Pointers as dada members
Friend functions.
Programming with ANSI C ++
Chapter 14: More About Classes.
2 Chapter Classes & Objects.
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.
Static Members and Methods
CISC181 Introduction to Computer Science Dr
Review: Two Programming Paradigms
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 Using C++
This pointer, Dynamic memory allocation, Constructors and Destructor
More Object Oriented Programming
CMSC 202 Static Methods.
More on Classes Classes may have constructors which are called when objects are created and destructors which are called when objects are destroyed. Classes.
Defining Your Own Classes Part 1
Basic C++ What’s a declaration? What’s a definition?
Corresponds with Chapter 7
Classes & Objects: Examples
Variables and Their scope
Objects with Functions and Arrays
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Function “Inputs and Outputs”
Submitted By : Veenu Saini Lecturer (IT)
CPS120: Introduction to Computer Science
Object Oriented Programming (OOP) Lecture No. 11
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Constructors & Destructors
Object Oriented Programming (OOP) Lecture No. 12
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
SPL – PS3 C++ Classes.
Presentation transcript:

C++ functions can be declared to use default values when none are supplied Class Circle { Float xc, yc, radius; Circle(float x, float y, float r=1.0); … } Example usage: Circle c1(1.0, 1.0); // omit def param; use 1.0 Circle c2(1.0, 1.0, 2.0); // supply “custom” value CS-1030 Dr. Mark L. Hornick

Review: C++ parameter passing Passing entire objects by value in C++ is not always desirable Objects can be big, leading to inefficiency Copy constructor must be called to make the copy of the object… Passing by reference or by address eliminates the need to copy entire objects Only the 4-byte address is copied Java only passes object references CS-1030 Dr. Mark L. Hornick

Review: parameter passing by object, reference, and address Review: Here’s a declaration of a method that passes an object by value: void printName(Employee e); Usage: printName( anEmp ); Here’s the modified declaration of the method to tell it to pass the object by reference instead: void printName(Employee& e); Usage: printName( anEmp ); // same as above Here’s the modified declaration of the method to tell it to pass the object by address instead: void printName(Employee* pe); Usage: printName( &anEmp ); // address of object CS-1030 Dr. Mark L. Hornick

Passing by reference or address – always advantageous? Useful if you want a method to be able to modify the variable you are passing to the method But what if you want to prevent a method from modifying the variable? CS-1030 Dr. Mark L. Hornick

The const specifier Prevents objects passed by reference or address from being changed within a method Here’s the modified declaration of the method that passes by reference: void printName(const Employee& e); Usage: printName( anEmp ); Here’s the modified declaration of the method that passes by address: void printName(const Employee* pe); Usage: printName( &anEmp ); CS-1030 Dr. Mark L. Hornick

Notation for const and pointers is tricky int x; const int* ptr1 = &x; // Can’t use ptr1 to change x, but can change ptr1 to point to another object int* const ptr2 = &x; // Can’t change ptr2 to another object, but can change value of x const int* const ptr2 = &x; // Can’t do either CS-1030 Dr. Mark L. Hornick

const data objects within classes class Employee { const int ID; Employee(); // def constructor Employee(int id); // constructor … } Constructor is still called, but const data cannot be initialized in the constructor So how do you set it? CS-1030 Dr. Mark L. Hornick

const data objects within classes - Initializers class Employee { const int ID; Employee():ID=0; Employee(int id):ID(id); … } Initializer Data members are initialized before the constructor is actually invoked CS-1030 Dr. Mark L. Hornick

const Member Functions May not alter data members of the class May not call other member functions that modify data members of the class Example CS-1030 Dr. Mark L. Hornick

static Data Members Same concept as Java’s static Belong to all objects of the same type (class) Shared by all instances Don’t need an object to exist Must be defined outside the class But declared inside Example CS-1030 Dr. Mark L. Hornick

static Member Functions Belong to the class Not the object! Don’t require an object to be used May only access static data members Example CS-1030 Dr. Mark L. Hornick

Who can you trust? Private members have limited scope …only methods in the same class can access them This can be circumvented any function (from any other class) can be given access to a private data member or method The outside function must be declared as a friend must be part of the class definition CS-1030 Dr. Mark L. Hornick

Friends An entire class can be made a friend of another class The class must be declared as a friend The declaration must be part of the class definition Friendship is not mutual If class A declares class B as a friend, the friendship is not automatically reciprocated Class B would have to declare class A as a friend CS-1030 Dr. Mark L. Hornick