A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
 2006 Pearson Education, Inc. All rights reserved Operator Overloading.
Classes Separating interface from implementation
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Overloading Overloading allows a function or operator to have a different meaning depending on the type of objects it is used on. Examples: operator+
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Object-Oriented Programming in C++
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
 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 Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Classes, Arrays & Pointers. Compiler & Linker expectations file1.cppfile2.cppfilen.cpp …. file1.ofile2.ofilen.o …. Linker application (executable) Compiler.
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.
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.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Learners Support Publications Constructors and Destructors.
Yan Shi CS/SE 2630 Lecture Notes
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
By Muhammad Waris Zargar
Programming with ANSI C ++
Constructor & Destructor
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
The dirty secrets of objects
group work #hifiTeam
Automatics, Copy Constructor, and Assignment Operator
Automatics, Copy Constructor, and Assignment Operator
Constructors and Destructors
9-10 Classes: A Deeper Look.
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
Review of Function Overloading
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors & Destructors
9-10 Classes: A Deeper Look.
Presentation transcript:

A C LOSER L OOK AT C LASSES 1

A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient that the types just be physically similar – their type names must be the same. By default, when one object is assigned to another, a bitwise copy of all the data members is made. Including compound data structures like arrays. Creates problem when member variables point to dynamically allocated memory and destructors are used to free that memory. Solution: Copy constructor (to be discussed later) Example: assign-object.cpp 2

P ASSING O BJECTS TO F UNCTIONS Objects can be passed to functions as arguments in just the same way that other types of data are passed. By default all objects are passed by value to a function. Address of an object can be sent to a function to implement call by reference. Examples: From book 3

P ASSING O BJECTS TO F UNCTIONS In call by reference, as no new objects are formed, constructors and destructors are not called. But in call value, while making a copy, constructors are not called for the copy but destructors are called. Can this cause any problem in any case? Yes. Solution: Copy constructor (discussed later) Example : obj-passing1.cpp, obj-passing2.cpp, obj- passing-problem.cpp 4

R ETURNING O BJECTS FROM F UNCTIONS The function must be declared as returning a class type. When an object is returned by a function, a temporary object (invisible to us) is automatically created which holds the return value. While making a copy, constructors are not called for the copy but destructors are called After the value has been returned, this object is destroyed. The destruction of this temporary object might cause unexpected side effects in some situations. Solution: Copy constructor (to be discussed later) Example: ret-obj-1.cpp, ret-obj-2.cpp, ret-obj-3.cpp 5

F RIEND F UNCTIONS A friend function is not a member of a class but still has access to its private elements. A friend function can be A global function not related to any particular class A member function of another class Inside the class declaration for which it will be a friend, its prototype is included, prefaced with the keyword friend. Why friend functions ? Operator overloading Certain types of I/O operations Permitting one function to have access to the private members of two or more different classes 6

F RIEND F UNCTIONS 7 class MyClass { int a; // private member public: MyClass(int a1) { a = a1; } friend void ff1(MyClass obj); }; // friend keyword not used void ff1(MyClass obj) { cout << obj.a << endl; // can access private member ‘a’ directly MyClass obj2(100); cout << obj2.a << endl; } void main() { MyClass o1(10); ff1(o1); }

F RIEND F UNCTIONS A friend function is not a member of the class for which it is a friend. MyClass obj(10), obj2(20); obj.ff1(obj2); // wrong, compiler error Friend functions need to access the members (private, public or protected) of a class through an object of that class. The object can be declared within or passed to the friend function. A member function can directly access class members. A function can be a member of one class and a friend of another. Example : friend1.cpp, friend2.cpp, friend3.cpp 8

F RIEND F UNCTIONS 9 class YourClass; // a forward declaration class MyClass { int a; // private member public: MyClass(int a1) { a = a1; } friend int compare (MyClass obj1, YourClass obj2); }; class YourClass { int a; // private member public: YourClass(int a1) { a = a1; } friend int compare (MyClass obj1, YourClass obj2); }; void main() { MyClass o1(10); YourClass o2(5); int n = compare(o1, o2); // n = 5 } int compare (MyClass obj1, YourClass obj2) { return (obj1.a – obj2.a); }

F RIEND F UNCTIONS 10 class YourClass; // a forward declaration class MyClass { int a; // private member public: MyClass(int a1) { a = a1; } int compare (YourClass obj) { return (a – obj.a) } }; class YourClass { int a; // private member public: YourClass(int a1) { a = a1; } friend int MyClass::compare (YourClass obj); }; void main() { MyClass o1(10); Yourclass o2(5); int n = o1.compare(o2); // n = 5 }

C ONVERSION F UNCTION Used to convert an object of one type into an object of another type. A conversion function automatically converts an object into a value that is compatible with the type of the expression in which the object is used. General form: operator type() {return value;} type is the target type and value is the value of the object after conversion. No parameter can be specified. Must be a member of the class for which it performs the conversion. Examples : From Book. 11 Department of CSE, BUET

C ONVERSION F UNCTION Department of CSE, BUET 12 #include using namespace std; class coord { int x, y; public: coord(int i, int j){ x = i; y = j; } operator int() { return x*y; } }; int main { coord o1(2, 3), o2(4, 3); int i; i = o1; // automatically converts to integer cout << i << ‘\n’; i = o2; // automatically converts to integer cout << i << ‘\n’; return 0; }

C ONVERSION F UNCTION Suppose we have the following two classes: Cartesian Coordinate: CCoord Polar Coordinate: PCoord Can we use conversion function to perform conversion between them? CCoord c(10, 20); PCoord p(15, 120); p = c; c = p; 13 Department of CSE, BUET

S TATIC CLASS MEMBERS A class member can be declared as static Only one copy of a static variable exists – no matter how many objects of the class are created All objects share the same variable It can be private, protected or public A static member variable exists before any object of its class is created In essence, a static class member is a global variable that simply has its scope restricted to the class in which it is declared 14

S TATIC CLASS MEMBERS When we declare a static data member within a class, we are not defining it Instead, we must provide a definition for it elsewhere, outside the class To do this, we re-declare the static variable, using the scope resolution operator to identify which class it belongs to All static member variables are initialized to 0 by default 15

S TATIC CLASS MEMBERS The principal reason static member variables are supported by C++ is to avoid the need for global variables Member functions can also be static Can access only other static members of its class directly Need to access non-static members through an object of the class Does not have a this pointer Cannot be declared as virtual, const or volatile static member functions can be accessed through an object of the class or can be accessed independent of any object, via the class name and the scope resolution operator Usual access rules apply for all static members Example : static.cpp 16

S TATIC CLASS MEMBERS 17 class myclass { static int x; public: static int y; int getX() { return x; } void setX(int x) { myclass ::x = x; } }; int myclass::x = 1; int myclass::y = 2; void main ( ) { myclass ob1, ob2; cout << ob1.getX() << endl; // 1 ob2.setX(5); cout << ob1.getX() << endl; // 5 cout << ob1.y << endl; // 2 myclass::y = 10; cout << ob2.y << endl; // 10 // myclass::x = 100; // will produce compiler error }

CONST M EMBER F UNCTIONS AND MUTABLE When a class member is declared as const it can’t modify the object that invokes it. A const object can’t invoke a non- const member function. But a const member function can be called by either const or non- const objects. If you want a const member function to modify one or more member of a class but you don’t want the function to be able to modify any of its other members, you can do this using mutable. mutable members can modified by a const member function. Examples : From Book. 18 Department of CSE, BUET

L ECTURE C ONTENTS Teach Yourself C++ Chapter 3 (Full, with exercises) Chapter 13 (13.2,13.3 and 13.4) 19