1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.

Slides:



Advertisements
Similar presentations
Introduction to C++ Templates Speaker: Bill Chapman, C++ developer, financial company in Mid-Manhattan. Can be reached via: ' This.
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Computer Science 1620 Function Scope & Global Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Templates. Class templates – why? Writing programs we often use abstract data types such as stack, queue or tree. Implementations of these types may be.
OOP Spring 2006 – Recitation 31 Object Oriented Programming Spring 2006 Recitation 3.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
Functions, Pointers, Structures Keerthi Nelaturu.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Object Oriented Programming (OOP) Lecture No. 11.
Object Oriented Programming (OOP) Lecture No. 10.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
1 Becoming More Effective with C++ … Day Two Stanley B. Lippman
Template Lecture 11 Course Name: High Level Programming Language Year : 2010.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Previous lecture Introduction to OOP and C++ Data Abstraction –String example.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Another Example: Complex Class #ifndef _Complex_H #define _Complex_H class Complex { float re, im; // by default private public: Complex(float x = 0,
C++ Functions A bit of review (things we’ve covered so far)
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Motivation for Generic Programming in C++
Eine By: Avinash Reddy 09/29/2016.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Topic: Classes and Objects
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.
Introduction to C++ Systems Programming.
FUNCTIONS In C++.
Introduction to Classes
Pointers and Pointer-Based Strings
Chapter 15: Overloading and Templates
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ for Engineers and Scientists Second Edition
Pass by Reference, const, readonly, struct
Introduction to Classes
Chapter 6 Methods: A Deeper Look
Local Variables, Global Variables and Variable Scope
Classes and Objects.
C++ Constructor Insanity CSE 333 Summer 2018
Pointers and Pointer-Based Strings
Submitted By : Veenu Saini Lecturer (IT)
C++ Constructor Insanity CSE 333 Autumn 2018
C++ Constructor Insanity CSE 333 Winter 2019
Destructors, Copy Constructors & Copy Assignment Operators
Destructors, Copy Constructors & Copy Assignment Operators
Class Circle { Float xc, yc, radius;
SPL – PS3 C++ Classes.
Presentation transcript:

1 Lecture 3 More about C++

2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static members –Namespace OperatorsOperators

3 3.5 More about classes

4 Init List Class String } char* chars; int length; public:String(); char* getString(); void setString(char* value); int equals(String *other); } String::String(){ chars = NULL; length = 0; } Constructors order First, the fields are created Second, the object itself is created

5 Init List String::String(): chars(NULL), length(0){} Class String } char* chars; int length; public:String(); char* getString(); void setString(char* value); int equals(String *other); }

6 MACROs in C (reminder)‏ Macro in C –Advantages Global constants Common functionality without function call overhead Common functionality on variety of types –Disadvantage Do not obey scope rules Type-unsafe constructs

7 C++ substitute to MACRO C++ provides substitutes for Macros –Inline functions – to eliminate function call overhead –Const – to substitute MACRO constants –Template functions – for type-safe common functionality on different types (later in this course…)‏

8 Inline functions class String{ char* chars; int length; public: String() ; String() : chars(NULL), length(0){}; char* getString() {return chars;}; void setString(char* value); int equals(String *other); } inline int max(int a, int b)‏ { return a < b ? b : a; }

9 Const member functions class String{ char* chars; int length; public: ; String() : chars(NULL), length(0){}; constconst const char* getString() const {return chars;}; void setString(char* value); const int equals(const String *other); } Indicates that this function does not modify the state of String The compiler will catch accidental attempts to violate this promise The const is part of the function declaration A const member function can be invoked for both const and non- const objects.

10 Const and functions Const applied to –Arguments void f(const int *i); –Return values const int *f(); this –The this object void C::f() const;

11 Const variables To tell compiler something is constant To limit usage of a variable Example: const float PI = ; –Avoid accidental change if (PI = some_variable) … instead of if (PI == some_variable)‏ Let the compiler optimize

12 Const and pointers p p p const int* p; // int const* p; p++;//ok *p = 17; //error! int* const p; p++; //error! *p = 17; //ok

13 Default function parameters class complex { double re,im; public: complex (double r=0, double i=0) {re = r; im = i;} double real_part() const {return re;} double imag_part() const {return im;} double abs() const {return sqrt(re*re +im*im);} }; int main()‏ { Complex c; Complex c1(2,3); Complex c2(2,0); Complex c3(2); // same as (2,0)‏ Complex c4;// same as (0,0)‏ }

14 Static members class Complex { double r, theta; public: complex (double r=0, double i=0):re(r),im(i){} double real_part() const {return r*cos(theta);} double imag_part() const {return r*sin(theta);} double abs() const {return r;} double arg(double x, double y) const { return !x ? (y >=0 ? PI/2 : -(PI/2)) : atan2(y,x);} }; Since implementation details are hidden it is possible to reimplement the class complex to use polar coordinates

15 Static members Complex::arg()The function Complex::arg() above raises a few points: this –Its action is unrelated to any specific Complex object, yet, as a member function, it has a this z.arg(a,y) z –It’s unreasonable to execute z.arg(a,y) for evaluating an expression that is totally unrelated to z PI PI –We need PI as a data member, why each complex object should have its own copy of PI?

16 Static members (Cont.)‏ class Complex { double r, theta; public: static const doubel PI; complex (double r=0, double i=0): re(r), im(i), PI( ){} double real_part() const {return r*cos(theta);} double imag_part() const {return r*sin(theta);} double abs() const {return r;} static double arg(double x, double y)‏ { return !x ? (y >=0 ? PI/2 : -(PI/2)) : atan2(y,x);} }; Static data membersStatic data members are unique, and belong to the class Static member functionsthisStatic member functions does not have this parameter, hence, can directly access only static data members Static member function scope operatorStatic member function can be called either using an object of the class, or using the scope operator Cannot be const

17 Namespaces Suppose we have two versions of the function sqrt()‏ –One is implemented for speed –The other for accuracy We would like to use in the same project the two versions namespace std { double sqrt (double); double pow (double, double); } namespace alt { double sqrt (double); double pow (double, double); } double pyth(double x, double y)‏ { return std::sqrt(x*x + y*y)‏ }

18 Namespaces (Cont.)‏ Namespace can be nested Namespace can be extended Global access to a selected identifier in a namespace is achieved by a using declaration Global access to all names in a namespace is achieved by a using directive using alt:sqrt; using namespace std;