Classes Member Qualifiers

Slides:



Advertisements
Similar presentations
EC-241 Object-Oriented Programming
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. S. Kozaitis Summer Summary of Topics Related to Classes Class definition Defining member.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Dale Roberts Introduction to Java - Access Specifiers Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts 1 Classes Constructors & Destructors Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Object Oriented Programming (OOP) Lecture No. 11.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 17 - C++ Classes: Part II Outline 17.1Introduction.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Chapter 17 - C++ Classes: Part II Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
1 const and this Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
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++ 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?
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
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?
Department of Computer and Information Science, School of Science, IUPUI Operator Overloading Dale Roberts, Lecturer Computer Science, IUPUI
CSC241: Object Oriented Programming
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.
Object Oriented Programming (OOP) Lecture No. 8
CISC181 Introduction to Computer Science Dr
Object Oriented Programming using Java - Class Instance Variables
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
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?
Chapter 5 Classes.
This technique is Called “Divide and Conquer”.
Chapter 17 - C++ Classes: Part II
Lecture 4-7 Classes and Objects
Functions Declarations CSCI 230
Classes Access Specifiers
Advanced Programming Basics
More On Enumeration Types
Classes Copy Constructors
Department of Computer and Information Science, School of Science, IUPUI CSCI 265 Classes Dale Roberts, Lecturer Computer Science, IUPUI
Pointers Call-by-Reference CSCI 230
Static in Classes CSCE 121 J. Michael Moore.
Today’s Topic Const Ref:
Classes Static Members
Submitted By : Veenu Saini Lecturer (IT)
A Deeper Look at Classes
Object Oriented Programming (OOP) Lecture No. 11
Dale Roberts, Lecturer IUPUI
Classes Introduction CSCI 240
Constructors & Destructors
Object Oriented Programming (OOP) Lecture No. 12
Classes Class Data Members & Initializers
Presentation transcript:

Classes Member Qualifiers Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Classes Member Qualifiers Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu 8/15/2019

Member Functions -- Qualifiers inline Member Functions Function Bodies are Substituted for the Function Call At Compile Time All the Member Functions Defined within the Class Definition are Implicitly Inline If Defined Outside the Class Definition, inline Qualifier is Required Faster Execution static Member Functions Exist Before Any Class Instantiation -- Access Only the Static Class Members const Member Functions CANNOT Change Data Members The ONLY ONE that can Process Constant Objects Can also Process Non-Constant Objects 8/15/2019

inline Member Functions -- Example class student{ int ss, credits; public: int get_ss(){return ss;} //Implicitly "inline" inline int get_credits(); //Explicitly "inline" }; //Explicitly "inline" int student::get_credits() {return credits;} 8/15/2019

Nested Classes A Class Contained in Another Class Multiple Levels of Nesting Are Allowed Usual Scoping Rules Apply 8/15/2019

Nested Classes -- Example class one{ //Class "two" is nested inside the class "one" int a; public: class two{ int b; public: two(){b = 10;} void print_b() {cout << "two's b: " << b << endl;} }; two one_two; one(){a = 100;} void print_a() {cout << "one's a: " << a << endl;} }; 8/15/2019

Nested Classes -- Example main(){ one s1; //s1 is an object of "one" s1.print_a(); //a = 100 s1.one_two.print_b(); //b = 10 //Object of class "two" defined inside "one" one::two s2; s2.print_b(); //b = 10 } 8/15/2019

Constant Data Members Principle of least privilege Keyword const Only give objects permissions they need, no more Keyword const Specify that an object is not modifiable Any attempt to modify the object is a syntax error Example const Time noon( 12, 0, 0 ); Declares a const object noon of class Time and initializes it to 12 8/15/2019

Constant Member Functions const objects require const functions Member functions declared const cannot modify their object const must be specified in function prototype and definition Prototype: ReturnType FunctionName(param1,param2…) const; Definition: ReturnType FunctionName(param1,param2…) const { …} Example: int A::getValue() const { return privateDataMember }; Returns the value of a data member but doesn’t modify anything so is declared const Constructors / Destructors cannot be const They need to initialize variables, therefore modifying them 8/15/2019

Constant Members and Functions Member initializer syntax All data members can be initialized using member initializer syntax constructor for Increment is modified as follows: Increment::Increment( int c, int i ) : increment( i ) { count = c; } : increment( i ) initializes increment to i consts and references must be initialized using member initializer syntax Multiple member initializers Use comma-separated list after the colon 8/15/2019

const -- Example  class student{ int ss, credits; public: student():ss(0), credits(0){} //const member function cannot modify the data member int get_ss() const {return ss;} int get_credits(){return credits;} }; main(){ student s1; const student s2; s1.get_ss(); s1.get_credits(); s2.get_ss(); //constant object s2.get_credits(); //ERROR!!! Cannot call a non-constant function } 8/15/2019

Acknowledgements These slides were originally prepared by Rajeev Raje, modified by Dale Roberts. 8/15/2019