Programming in C++ Michal Brabec Petr Malý. Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor.

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
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.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
Class and Objects.
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];
Chapter 14: Overloading and Templates
C++ data types. Structs vs. Classes C++ Classes.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
C++ Templates Gordon College CPS212. Overview Templates Definition: a pattern for creating classes or functions as instances of the template at compile.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Structured Programming Instructor: Prof. K. T. Tsang Lecture 13:Object 物件 Oriented 面向 Programming (OOP)
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
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+
J. P. Cohoon and J. W. Davidson © 1997 McGraw-Hill, Inc. Templates Generic functions and classes.
Concordia TAV 2002 Comp5421_421 Comp5421 Object Oriented Programming Using C++ Efficiently Lecture 4 (2) Tianxiang Shen Summer 2002 Department of Computer.
Operator overloading and type convesions BCAS,Bapatla B.mohini devi.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
More C++ Features True object initialisation
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 12 - Templates Outline 12.1Introduction 12.2Function Templates 12.3Overloading Template Functions.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
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 Languages and Paradigms C++. C++ program structure  C++ Program: collection of files Header files CPP source files  Files contain class,
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
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.
1 Introduction to Object Oriented Programming Chapter 10.
Programming in C++ Michal Brabec Petr Malý. Standard Template Library Containers - vector, map, set, deque, list Algorithms - copy, replace, sort, find.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
1 Chapter 1 C++ Templates Readings: Sections 1.6 and 1.7.
Programming 2. Arrays & structure Arrays : allow you to define variables that combine several data items of the same kind. Structure : is another user.
Programming in C++ Michal Brabec Petr Malý. Inheritance class derived-class: access-specifier base-class {} Base & Derived class Implementation inheritance.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Chapter 2 Objects and Classes
Yan Shi CS/SE 2630 Lecture Notes
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
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?
A First C++ Class – a Circle
Chapter 4 Linked Lists.
Chapter 5 Classes.
Class: Special Topics Copy Constructors Static members Friends this
of functions, methods & operators
Memberwise Assignment / Initialization
group work #hifiTeam
understanding memory usage by a c++ program
Classes & Objects: Examples
Copy Constructor CSCE 121 J. Michael Moore.
Passing Arguments and The Big 5
Class: Special Topics Overloading (methods) Copy Constructors
Class: Special Topics 2 For classes using memory allocation
Types of Computer Languages
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Copy Constructor CSCE 121.
C++ data types.
Object Oriented Programming
Presentation transcript:

Programming in C++ Michal Brabec Petr Malý

Class / Struct Class / Struct consists of: data members function members constructors destructor copy constructor assignment operator other operators conversion operators

Copy Constructor ClassName(const ClassName& obj) { } All members are copied to the members of a new object Compiler generates default implementation Used together with assignment operator

Assignment Operator ClassName& operator=(const ClassName& obj); Compiler generates default implementation All members are assigned to members of a new object Used together with copy constructor

Task Use the solution of the second task (MyArray) from the last practicals Create a global function Replace which takes three arguments InputArray ElementToFind NewElement Function returns a new copy of MyArray where all ElementToFind are replaced for NewElement Verify your soulution using Test class as element type of your array

Shallow Copy class MyClass { public: int* member1; int member2; int member3; }; MyClass myClass1; myClass1.member1 = new int[3] { 1, 2, 3}; myClass1.member2 = 4; myClass1.member3 = 5; MyClass myClass2 = myClass1; myClass1.member1[0] = 1000; cout << myClass1.member1[0] << endl; // 1000 cout << myClass1.member1[1] << endl; // 2 cout << myClass1.member1[2] << endl; // 3 cout << myClass1.member2 << endl; // 4 cout << myClass1.member3 << endl; // 5 cout << myClass2.member1[0] << endl; // 1000 cout << myClass2.member1[1] << endl; // 2 cout << myClass2.member1[2] << endl; // 3 cout << myClass2.member2 << endl; // 4 cout << myClass2.member3 << endl; // 5 addr4 myClass1: addr4 myClass2: 5

Deep Copy class MyClass { public: int* member1; int member2; int member3; MyClass() = default; MyClass(const MyClass& obj) : member2(obj.member2), member3(obj.member3) { member1 = new int[3]; for (int i = 0; i < 3; ++i) member1[i] = obj.member1[i]; } }; MyClass myClass1; myClass1.member1 = new int[3] { 1, 2, 3}; myClass1.member2 = 4; myClass1.member3 = 5; MyClass myClass2 = myClass1; myClass1.member1[0] = 1000; cout << myClass1.member1[0] << endl; // 1000 cout << myClass1.member1[1] << endl; // 2 cout << myClass1.member1[2] << endl; // 3 cout << myClass1.member2 << endl; // 4 cout << myClass1.member3 << endl; // 5 cout << myClass2.member1[0] << endl; // 1 cout << myClass2.member1[1] << endl; // 2 cout << myClass2.member1[2] << endl; // 3 cout << myClass2.member2 << endl; // 4 cout << myClass2.member3 << endl; // 5 addr4 myClass1: addr4 myClass2:

Operators RetType operator op (Args) { } Defined as members of a class Some operators may be defined outside the class friend keyword is used to allow to access to private class members

Conversion Operators operator Type (Args) { } Defined as members of a class Return type is not at the beginning of the declaration Result is returned by value

Task Create operator + for MyArray class The operator concatenates two arrays Create a conversion operator to std::vector

Task (optional) Use code from previous practicals Create a program which loads the complex numbers from the file The program stores the number ordered by their absolute value Use STL functions / containers