C++ Classes, Constructor & Object Oriented Programming

Similar documents
Constructor - example

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

Object Oriented Programming

Introduction to Programming session 24


Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Chapter 9 Classes : A Deeper Look, Part 1

Classes: A Deeper Look

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Computer Programming with C++ (21)

Introduction Of Classes ( OOPS )

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Object Oriented Design

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

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

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

What is Class? Remember

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

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Lecture 5. Function Pointers

Introduction to Classes

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

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

l A class in C++ is similar to a structure. l A class contains members: - variables AND - public: accessible outside the class.

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


G205 Fundamentals of Computer Engineering. CLASS 3, Wed. Sept Stefano Basagni Fall 2004 M-W, 1:30pm-3:10pm

Data Structures using OOP C++ Lecture 3

Object Oriented Design

Chapter 10 Introduction to Classes

! A class in C++ is similar to a structure. ! A class contains members: - variables AND. - public: accessible outside the class.

The University Of Michigan. EECS402 Lecture 06. Andrew M. Morgan. Savitch Ch. 6 Intro To OOP Classes Objects ADTs.

CS304 Object Oriented Programming Final Term

Lecture 18 Tao Wang 1

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

Introduction to Inheritance

EL2310 Scientific Programming

57:017, Computers in Engineering C++ Classes

Programming, numerics and optimization

CS6456 OBJECT ORIENTED PROGRAMMING L T P C

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Topics. Constructor. Constructors

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

Introduction to Classes and Objects How to manage data and actions together. Slides #10: Chapter

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

Object-Oriented Programming (OOP) Fundamental Principles of OOP

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

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

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Short Notes of CS201

W8.1 Continuing Classes friend Functions and friend Classes Using the this Pointer Cascading Function Calls

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CS201 - Introduction to Programming Glossary By

Fundamentals of Programming Session 24

Classes and Data Abstraction

Object-Oriented Programming Concepts

Abstraction in Software Development

Object Oriented Programming. Solved MCQs - Part 2

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

Fast Introduction to Object Oriented Programming and C++

Object Oriented Programming COP3330 / CGS5409

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

C++ PROGRAMMING LANGUAGE: CLASSES. CAAM 519, CHAPTER 13

IS 0020 Program Design and Software Tools

clarity. In the first form, the access specifier public : is needed to {

CS201 Latest Solved MCQs

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

AN OVERVIEW OF C++ 1

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

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

A Deeper Look at Classes

An Introduction to C++

CSE 303: Concepts and Tools for Software Development

Interview Questions of C++

Class 15. Object-Oriented Development from Structs to Classes. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

System Design and Programming II

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

Lecture 7. Log into Linux New documents posted to course webpage

! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

OBJECT ORIENTED PROGRAMMING

CLASSES AND OBJECTS IN JAVA

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

Topic 10: Introduction to OO analysis and design

C++ : Object Oriented Features. What makes C++ Object Oriented

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

Inheritance, and Polymorphism.

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

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

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

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Transcription:

C++ Classes, Constructor & Object Oriented Programming

Object Oriented Programming Programmer thinks about and defines the attributes and behavior of objects. Often the objects are modeled after real-world entities. Very different approach than function-based programming (like C).

Object Oriented Programming Object-oriented programming (OOP) Encapsulates data (attributes) and functions (behavior) into packages called classes. So, Classes are user-defined (programmer-defined) types. Data (data members) Functions (member functions or methods) In other words, they are structures + functions

Classes in C++ A class definition begins with the keyword class. The body of the class is contained within a set of braces, ; (notice the semi-colon). class class_name... ; Any valid identifier Class body (data member + methods)

Classes in C++ Within the body, the keywords private: and public: specify the access level of the members of the class. the default is private. Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

Classes in C++ class class_name private: public: ; private members or methods Public members or methods

Classes in C++ Member access specifiers public: can be accessed outside the class directly. private: The public stuff is the interface. Accessible only to member functions of class Private members and methods are for internal use only.

Class Example This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) class Circle private: double radius; public: void setradius(double r); double getdiameter(); double getarea(); double getcircumference(); ; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)

Creating an object of a Class Declaring a variable of a class type creates an object. You can have many variables of the same type (class). Instantiation Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code You can instantiate many objects from a class type. Ex) Circle c; Circle *c;

Special Member Functions Constructor: Public function member called when a new object is created (instantiated). Initialize data members. Same name as class No return type Several constructors Function overloading

Special Member Functions class Circle private: double radius; public: Circle(); Circle(int r); void setradius(double r); double getdiameter(); double getarea(); double getcircumference(); ; Constructor with no argument Constructor with one argument

Implementing class methods Class implementation: writing the code of class methods. There are two ways: 1. Member functions defined outside class Using Binary scope resolution operator (::) Ties member name to class name Uniquely identify functions of particular class Different classes can have member functions with same name Format for defining member functions ReturnType ClassName::MemberFunctionName( )

Implementing class methods 2. Member functions defined inside class Do not need scope resolution operator, class name; class Circle private: double radius; public: Circle() radius = 0.0; Circle(int r); void setradius(double r)radius = r; double getdiameter() return radius *2; double getarea(); double getcircumference(); ; Defined inside class

class Circle private: double radius; public: Circle() radius = 0.0; Circle(int r); void setradius(double r)radius = r; double getdiameter() return radius *2; double getarea(); double getcircumference(); ; Circle::Circle(int r) radius = r; double Circle::getArea() return radius * radius * (22.0/7); double Circle:: getcircumference() return 2 * radius * (22.0/7); Defined outside class

Accessing Class Members Operators to access class members Identical to those for structs Dot member selection operator (.) Object Reference to object Arrow member selection operator (->) Pointers

class Circle private: double radius; public: Circle() radius = 0.0; Circle(int r); void setradius(double r)radius = r; double getdiameter() return radius *2; double getarea(); double getcircumference(); ; Circle::Circle(int r) radius = r; double Circle::getArea() return radius * radius * (22.0/7); double Circle:: getcircumference() return 2 * radius * (22.0/7); void main() Circle c1,c2(7); The first The second constructor is constructor is called called cout<< The area of c1: <<c1.getarea()<< \n ; Since radius is a private class data member //c1.raduis = 5;//syntax error c1.setradius(5); cout<< The circumference of c1: << c1.getcircumference()<< \n ; cout<< The Diameter of c2: <<c2.getdiameter()<< \n ;

class Circle private: double radius; public: Circle() radius = 0.0; Circle(int r); void setradius(double r)radius = r; double getdiameter() return radius *2; double getarea(); double getcircumference(); ; Circle::Circle(int r) radius = r; double Circle::getArea() return radius * radius * (22.0/7); double Circle:: getcircumference() return 2 * radius * (22.0/7); void main() Circle c(7); Circle *cp1 = &c; Circle *cp2 = new Circle(7); cout<< The are of cp2: <<cp2->getarea();

Destructors Destructors Special member function Same name as class Preceded with tilde (~) No arguments No return value Cannot be overloaded Before system reclaims object s memory Reuse memory for new objects Mainly used to de-allocate dynamic memory locations

Another class Example This class shows how to handle time parts. Destructor class Time private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printtime(); void settime(int h,int m,int s); int gethour()return *hour; int getminute()return *minute; int getsecond()return *second; void sethour(int h)*hour = h; void setminute(int m)*minute = m; void setsecond(int s)*second = s; ~Time(); ;

Time::Time() hour = new int; minute = new int; second = new int; *hour = *minute = *second = 0; Dynamic locations should be allocated to pointers first Time::Time(int h,int m,int s) hour = new int; minute = new int; second = new int; *hour = h; *minute = m; *second = s; void Time::setTime(int h,int m,int s) *hour = h; *minute = m; *second = s;

void Time::printTime() cout<<"the time is : ("<<*hour<<":"<<*minute<<":"<<*second<<")" <<endl; Destructor: used here to deallocate memory locations Time::~Time() delete hour; delete minute;delete second; void main() Time *t; t= new Time(3,55,54); t->printtime(); Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue t->sethour(7); t->setminute(17); t->setsecond(43); t->printtime(); delete t; When executed, the destructor is called

Reasons for OOP 1. Simplify programming 2. Interfaces Information hiding: Implementation details hidden within classes themselves 3. Software reuse Class objects included as members of other classes