More C++ : Vectors, Classes, Inheritance, Templates

Similar documents
More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

And Even More and More C++ Fundamentals of Computer Science

Introduction Of Classes ( OOPS )

18. Polymorphism. Object Oriented Programming: Pointers to base class // pointers to base class #include <iostream> using namespace std;

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Inheritance, and Polymorphism.

Object Oriented Programming. A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Object Oriented Programming. Solved MCQs - Part 2

Chapter 1: Object-Oriented Programming Using C++

C++ Important Questions with Answers

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CS 162, Lecture 25: Exam II Review. 30 May 2018

Java Object Oriented Design. CSC207 Fall 2014

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

C++ Inheritance and Encapsulation

CLASSES AND OBJECTS IN JAVA

C++ (classes) Hwansoo Han

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE


C++ (Non for C Programmer) (BT307) 40 Hours

Object-oriented Programming. Object-oriented Programming

CMSC 132: Object-Oriented Programming II

Fast Introduction to Object Oriented Programming and C++

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Lesson Plan. Subject: OBJECT ORIENTED PROGRAMMING USING C++ :15 weeks (From January, 2018 to April,2018)

Ch02. True/False Indicate whether the statement is true or false.

OBJECT ORIENTED PROGRAMMING

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Data Abstraction. Hwansoo Han

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Object Oriented Programming(OOP).

C++ Memory Map. A pointer is a variable that holds a memory address, usually the location of another variable in memory.

Module 10 Inheritance, Virtual Functions, and Polymorphism

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Interview Questions of C++

VALLIAMMAI ENGINEERING COLLEGE

CS-202 Introduction to Object Oriented Programming

Object-Oriented Programming

Midterm Exam 5 April 20, 2015

Seminar on Simulation of Telecommunication Sistems. Introduction to object-oriented programming: C++

COEN244: Class & function templates

6.096 Introduction to C++ January (IAP) 2009

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Chapter 10 Introduction to Classes

CS250 Final Review Questions

ITI Introduction to Computing II

CS250 Final Review Questions

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Introduction to Classes

Short Notes of CS201

Data Structures (INE2011)

Chapter 9 :: Data Abstraction and Object Orientation

Polymorphism. Zimmer CSCI 330

CS201 - Introduction to Programming Glossary By

VIRTUAL FUNCTIONS Chapter 10

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Fundamental Concepts and Definitions

ITI Introduction to Computing II

Object Oriented Programming

Lecture 18 Tao Wang 1

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Object Oriented Programming in C#

Lecture 5: Inheritance

XII- COMPUTER SCIENCE VOL-II MODEL TEST I

KLiC C++ Programming. (KLiC Certificate in C++ Programming)

An Introduction to C++

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

And Even More and More C++ Fundamentals of Computer Science

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

Chapter 6 Introduction to Defining Classes

CSE 307: Principles of Programming Languages

Abstraction in Software Development

Object Oriented Software Design II

C++ Inheritance and Encapsulation

C++ 8. Constructors and Destructors

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

Object Oriented Software Design II

Data Structure. Recitation III

CS250 Final Review Questions

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

INHERITANCE: EXTENDING CLASSES

Tokens, Expressions and Control Structures

Object-Oriented Principles and Practice / C++

Object Oriented Programming CS250

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Programming, numerics and optimization

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Lecture 7: Classes and Objects CS2301

Transcription:

Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit memory allocation contains methods, such as size() with content from cplusplus.com, codeguru.com 2 Vectors C++ Standard Arrays vs. Vectors using vectors must include <vector> template, so must be instantiated with type qualified with std:: can be simplified in small projects 3 4

Vector Length previous program does not check for valid index, which enhances performance using at function will check index Vector Length vectors can grow certain amount of space allocated initially once that space runs out, new space is allocated and the values are copied over 5 6 Vector Size use pushback(el) to grow the size dynamically use resize to set or reset the size of the array Vector Size use the size() method for loops for (i = 0; i < array.size(); i++) array[i] = 0; 7 8

classes fancy expanded concept of data structures (functions) object instantiation of a type/variable class/object defined with keyword class (or struct) members are listed under specifiers private members accessible only from within the class protected members accessible to class or classes public members accessible anywhere the object is visible by default, access is 9 10 members are accessed through declares a class, Rectangle declares an object, rect class contains 4 members 2 private data 2 public methods (declarations only, not definitions) methods can be accessed directly using. operator similar to 11 12

with 2 variables notes: declaration vs. definition inline function encapsulation data hiding notes: each object has its own set of data/methods no parameters needed for call to area output area: 12 output rect area: 12 rectb area: 30 13 14 what would happen if we called area before setting values? undetermined result constructors automatically called when a new object is initializes values, allocates, etc. constructor name same as class name no return type cannot be called notes: results same as before set_values omitted values passed to constructor output rect area: 12 rectb area: 30 15 16

constructors can be different number of parameters different parameter types default constructor defined if no other constructor defined takes no parameters called when object is declared but no parameters are passed to the constructor cannot call default constructor with parentheses represents a declaration member initialization can be done in constructor body or member can be defined normally or with member initialization 17 18 for defined or by default for member objects (whose type is a ) if not initialized after the colon, they are defaultconstructed default construction may not be possible if no default constructor defined for class use member initialization list instead Cylinder class has member of type class Circle and needs to call Circle constructor in member initialization list 19 20

operator overloading allows operators, such as + or *, to be defined for userdefined types defined like member functions, but prepended with keyword operator operator overloading : equivalent 21 22 this pointer to used within a class method to refer to the object that called it templates parameterized class Rectangle::Rectangle (int width, int height) { this -> width = width; this -> height = height; } can be used to store elements of type int or type float 23 24

destructor opposite of constructor ends performs, such as memory deallocation returns nothing, not even void name same as class name, but preceded by ~ implicit default destructor provided if none defined destructor 25 26 Inheritance inheritance allows classes to be classes retain characteristics of the base class avoids replicated code by allowing common properties to be contained in one class and then used by other classes Inheritance inheritance derived classes contain width, height, set_values output Polygon contains common members; Rectangle and Triangle contain common members plus specific features 27 28

Inheritance inheritance access types and inheritance Virtual Methods virtual methods can be redefined in classes, while preserving its calling signature declared with keyword virtual inherited members have same access permissions as in base class since 29 30 Virtual Methods virtual method area declared virtual derived classes will redefine it Virtual Methods virtual methods if virtual keyword removed, all derived class calls to area method through pointers to base class would return 0 virtual methods redefined in derived classes non-virtual methods can also be redefined in derived classes but, if virtual, a to the base class can access the redefined virtual method in the derived class a class that declares or inherits a virtual function is note that Poly is a class, too, and objects can be declared with it 31 32

Virtual Methods abstract base class similar to base class in previous can only be used as base classes can have virtual methods without pure virtual function appended with =0 abstract base class cannot be used to declare can be used to create to it and take advantage of polymorphic features 33 34 Inheritance abstract base class 35