CSC1322 Object-Oriented Programming Concepts

Similar documents
Inheritance, and Polymorphism.

Classes in C++98 and C++11

Chapter 1: Object-Oriented Programming Using C++

a data type is Types

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Pointers, Dynamic Data, and Reference Types

Fast Introduction to Object Oriented Programming and C++

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

Constructor - example

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

Assignment of Structs

Come and join us at WebLyceum

Scope. Scope is such an important thing that we ll review what we know about scope now:

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Introducing C++ to Java Programmers

Dynamic Data Structures

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

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CLASSES AND OBJECTS IN JAVA

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

Function Overloading

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

2 ADT Programming User-defined abstract data types

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Functions, Arrays & Structs

CPSC 427: Object-Oriented Programming

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

C++ 8. Constructors and Destructors

CSC 211 Intermediate Programming. Arrays & Pointers

An Introduction to C++

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

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Object-Oriented Programming

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Programming II (CS300)

Object Reference and Memory Allocation. Questions:

Programming II (CS300)

Recharge (int, int, int); //constructor declared void disply();

CS24 Week 3 Lecture 1

Object Oriented Design

AN OVERVIEW OF C++ 1

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

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

CS 376b Computer Vision

CSCE 110 PROGRAMMING FUNDAMENTALS

COMP 2355 Introduction to Systems Programming

Object-Oriented Principles and Practice / C++

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

Pointers II. Class 31

Programming C++ Lecture 3. Howest, Fall 2012 Instructor: Dr. Jennifer B. Sartor

การทดลองท 8_2 Editor Buffer Array Implementation

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

10. Abstract Data Types

Variables, Memory and Pointers

Lecture 18 Tao Wang 1

Assumptions. History

Implementing an ADT with a Class

CPSC 427: Object-Oriented Programming

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

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms

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

Benefits of Data Encapsulation. Internal and External Views. Lecture 4 : Data Abstraction. Data Encapsulation. Information Hiding

Object Oriented Programming: Inheritance Polymorphism

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Object-Oriented Programming for Scientific Computing

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

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

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

CMPT 135: Midterm Answer Key

Exam 3 Chapters 7 & 9

Intermediate Programming, Spring 2017*

Introduction to Programming Using Java (98-388)

Lecture 8: Object-Oriented Programming (OOP) EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

CSCI 104 Inheritance. Mark Redekopp David Kempe

[0569] p 0318 garbage

CMSC 132: Object-Oriented Programming II

Outline. 1 About the course

C11: Garbage Collection and Constructors

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

For Teacher's Use Only Q No Total Q No Q No

Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University


Extending Classes (contd.) (Chapter 15) Questions:

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

CS201 Some Important Definitions

Intermediate Programming & Design (C++) Classes in C++

AIMS Embedded Systems Programming MT 2017

More Tutorial on C++:

Link-Based Implementations. Chapter 4

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

WHAT POINTERS REALLY ARE

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

Transcription:

CSC1322 Object-Oriented Programming Concepts Instructor: Yukong Zhang February 18, 2016 Fundamental Concepts: The following is a summary of the fundamental concepts of object-oriented programming in C++. 1. Class: A data type used to represent a new structured data type. A class is also regarded as a template through which individual objects are created. Example syntax: class Student Student(); string getname(); void setname(); string name; float gpa; 2. Object: A variable declared with a new class type. An object is an instance of a class type. Student st; Variable st above is an instance or object of class Student. It has a set of attributes and functions declared in class Student. 3. Constructor: A special function dened in a class that is used to set initial values for the member variables of the class. It is invoked or called implicitly when an object is being created. Constructors do not return values and the names of the constructors should be the same as the class name. 4. More about constructors: a class can have multiple constructors. The constructor without parameters is called the default constructor. The constructors with parameters are called parameterized constructors. Only one constructor with matching parameter list will be called when an object is being created. 1

class Student Student(); //default constructor Student(string nm, float g, int a); //parameterized constructor string getname(); void setname(); string name; float gpa; The denition of constructors: Student::Student() name = "John"; gpa = 3.0; age = 18; Student::Student(string nm, float g, int a) name = nm; gpa = g; age = a; 5. Accessiblity: There are three levels of accessibilty: private, protected, and public. When a member variable is dened as private, it can be accessed only by the member functions of the same class. protected" is accessible to the functions dened in child classes. public" is accessible from anywhere. 6. Member variables: Member functions of a class can directly access and manipulate private member variables of the same class. When a member variable is mentioned in a member function, it is the calling object's member variable. For clarity, sometimes we use this" to refer to the calling object in member functions by this->membervariable." Note that this" is a pointer to the calling object. 7. Member functions: Member functions can be called from other member functions of the same class. Outside of a class, public member functions must be called by an object of the same type. Page 2

8. Client programs: The programs that use classes are the client or application programs such as a program with main( ) function. The client programs can access/refer ONLY the public members (variables and functions) of a class. Student st1; Student st2("shannon", 3.6, 21); st1.setname("david"); cout << st2.getname(); //The default constructor //The non-default constructor 9. Declaration and definition: The declarations of a class provide a list of member variables and function prototypes. The denitions mean the implementations of the member functions of the class. 10. Information hiding: Once the member variables and/or functions of a class are declared as private, they cannot be directly accessed or referred from client programs. In other words, they are hidden from the client programs. The only way to access or manipulate the private members of a class is through the public functions (interfaces) dened in the class. In the real-world, the creators of classes and the users of the classes are two dierent group of people or software developers. The users typically don't care much about how the classes are implemented. As long as they know how the public functions (only prototypes)/interfaces of classes are declared, they should be able to use/call them. Therefore, any implmentation changes made to a class or its member functions don't aect the use of the class by the users if the prototypes of the public functions remain the same. 11. POINTERS: A pointer variable stores memory addresses as its value. Sample pointer variable notations: int a = 5; int * p; p = &a; //variable p stores the memory address of a cout << *p << endl; //asterisk * is called dereference operator. 5 *p = 10; //Indirectly change variable a's value to 10 p = NULL; //NULL is a special value for pointer variables 12. REFERENCE VARIABLE: A reference variable is an alternative name (alias) for an object. Page 3

int a = 8; int & p = a; cout << a << endl; //8 p = 10; //indirectly change the value variable a to 10 cout << a << endl; //10 13. ARRAY AND POINTER: An array can be manipulated through pointers because the fact that the name of an array variable itself is considered as a pointer variable pointing to the memory address of the rst array element sometimes called base address of the array. For example. int a[5] = 12, 2, 5, 4, 8; int * p; p = a; //p points to the first element of array a cout << *a << endl; //print out the value of the first element of array a cout << a[0] << endl; //it is equivalent to the preceding cout cout << *(a + 3) << endl; //equivalent to a[3] 14. DYNAMIC DATA: A variable or object can be created dynamically through new" operator. The memory space allocated to dynamic data will be in heap or free store memory region instead of stack region where static data is located. Dynamic data need to be deleted explicitly by programmers to release its member space to the system for reuse when they are no longer used in programs. For example, s2 in the main() below. class Student Student() age = 20; Student(int a) age = a; ~Student() cout << "Destructor is called" << endl; void print() cout << age << endl; ; Student s; s.print(); Page 4

Student * s2 = new Student(22); s2->print(); delete s2; 15. DESTRUCTOR: A destructor is used to release the memory of an object when it is deleted. All static variables will be deleted automatically just before the containing functions return. All dynamic data are deleted explicitly by a delete statement. 16. COPY CONSTRUCTOR: The copy constructor is called when an object is being created with another object. A default copy constructor is provided by the system if the user doesn't provide one. The default one basically copies member variables component by component. In the following sample code, the copy constructor is called when s2 and s3 are being created. Student s1; Student s2(s1); Student s3 = s1; // Default constructor will be called // copy constructor will be called // copy constructor will be called 17. ASSIGNMENT OPERATOR= OVERLOADING: Assignment operator is normally used for arithmetic operations of primitive (built-in) data types. If it is used for objects, the default assignment operator = copies member variables component by component. If the default behavior is not desired, the user needs to re-dene the assignment operator = for the classes. #include <iostream> using namespace std; class Student Student() age = 20; cout << "Default constructor is called" << endl; Student(int a) Page 5

age = a; cout << "Parameterized constructor is called" << endl; Student(const Student & rhs) age = rhs.age + 10; cout << "Copy constructor is called" << endl; //Assignment operator = overloading Student & operator=(const Student & rhs) age = rhs.age + 4; cout << "Assignment operator = overloading function will be called" << endl; return * this; //Return a reference to itself ~Student() cout << "Destructor is called" << endl; void print() cout << age << endl; ; Student s1; //Default constructor will be called //Student s2(s1); //copy constructor will be called Student s2 = s1; //copy constructor will be called Student s3(25); //Parameterized constructor will be called Student * s4 = new Student(28); s2 = s1; //Assignment operator = overloading function will be called s1.print(); s2.print(); Page 6

s3.print(); s4->print(); delete s4; //Need to explicitly delete this dynamic data //Output: //Default constructor is called //Copy constructor is called //Parameterized constructor is called //Parameterized constructor is called //Assignment operator = overloading function will be called //20 //24 //25 //28 //Press any key to continue... (a) Shallow copy: When an object contains a dynamic data (that is, created through new operator) and is copied from or initialized from another object, default copy constructor copies member variables piece by piece from one object to the other. Since dynamic data are pointed by pointer type variables, the dynamic data were not copied but pointers. As such, a single copy of a dynamic data is pointed or referenced by two objects. When one object deletes the dynamic data, the other object cannot access it any more. (b) Deep copy: There are two copies of dynamic data, one for each object. 18. MEMORY LEAK: Memory leak occurs when a memory space cannot be accessed after an improper assignment involving pointer variables. Page 7