Introduction to Classes and Objects

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
1 A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1.
Constructors & Destructors Review CS 308 – Data Structures.
1 Introduction to C++ Programming Concept Basic C++ C++ Extension from C.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
1 C++ Structures Starting to think about objects...
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
C++ Review (3) Structs, Classes, Data Abstraction.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
CS1201: Programming Language 2 Classes and objects.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
1 Introduction to Object Oriented Programming Chapter 10.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 12 Classes and Abstraction
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Concepts of Constructors and Its Types
Review: Two Programming Paradigms
Introduction to Classes
Object Lifetime and Dynamic Objects
CS1201: Programming Language 2
Constructor & Destructor
Constructors & Destructors
This technique is Called “Divide and Conquer”.
C++ Classes & Object Oriented Programming
Object Oriented Analysis and Design
CS212: Object Oriented Analysis and Design
Introduction to Classes
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Starting to think about objects...
Constructors and destructors
Constructors and Destructors
CLASSES AND OBJECTS.
CPS120: Introduction to Computer Science
C++ Programming ㅎㅎ String OOP Class Constructor & Destructor.
COP 3330 Object-oriented Programming in C++
CS1201: Programming Language 2
Review: C++ class represents an ADT
CPS120: Introduction to Computer Science
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
Pointers, Dynamic Data, and Reference Types
Introduction to Classes and Objects
Presentation transcript:

Introduction to Classes and Objects CHAPTER 5 Introduction to Classes and Objects

Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM OBJECT Operations Data FUNCTION FUNCTION OBJECT Operations Data A list of tasks to perform. Viewed as a collection of interacting objects. Each object can be viewed as an independent machine with a distinct role or responsibility. Operations are closely associated with the objects, carry their own operators around with them . OBJECT Operations Data FUNCTION Function calls Messages passing

Review: C++ Data Types simple structured address integral enum floating float double long double array struct union class char short int long bool address pointer reference

Classes & Objects The class is the cornerstone of C++ Class: Object: It gives the C++ its identity from C It makes possible encapsulation, data hiding and inheritance Class: Consists of both data and methods Defines properties and behavior of a set of entities Object: An instance of a class A variable identified by a unique name Aim of class: a) provide the programmer with a tool for creating new types that can be used as conveniently as the built-in types (like float) b) user-defined type why? Separate the incidental details of the implementation from the properties essential to the correct use of it 2. Derived class, templates: organizing related classes that allow the programmer to take advantage of their relationships.

Define a Class Type class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; class class_name { permission_label: member; ... }; Header Body

Defining a member function with a parameter #include <iostream.h> class circle { private: double radius; public: void store(double); double area(void); void display(void); }; // member function definitions void circle::store(double r) { radius = r; } double circle::area(void) return 3.14*radius*radius; void circle::display(void) cout << “r = “ << radius << endl; int main(void) { circle c; // an object of circle class c.store(5.0); cout << "The area of circle c is " << c.area() << endl; c.display(); }

Constructors Constructor:– a function used to initialize the data of an object of a class Same name as class itself Cannot return anything, not even void A class may define more than one constructor With different parameter lists Default constructor has no parameters Called automatically When class object is declared as automatic variable By new operator

Constructor Example Constructor has same name as class and no return type Initialize data member

Constructor Example Destructor need so this class object can free dynamically allocated memory

Creating objects implicitly calls the constructor Constructor Example Creating objects implicitly calls the constructor

Defining the constructor class Circle { private: double radius; public: Circle(); Circle(int r); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; Constructor with no argument – Default Constructor Constructor with one argument

Destructors Destructor:– a function used to clean up an object of a class prior to deleting that object Class name preceeded by '~' No parameters, no result Called automatically When function exits scope of automatic class object By delete operator

Destructors Example class string { private: char *s; int size; public: string(char *); // constructor ~string(); // destructor };   string::string(char *c) { size = strlen(c); s = new char[size+1]; strcpy(s,c); } string::~string() delete []s;

Composition: Objects as member of classes What is an object? OBJECT set of methods (public member functions) internal state (values of private data members) Operations Data

Declaration of an Object class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } main() { Rectangle r1; Rectangle r2; r1.set(5, 8); cout<<r1.area()<<endl; r2.set(8,10); cout<<r2.area()<<endl; }

Example #include <iostream.h> // member function definitions class circle { private: double radius; public: void store(double); double area(void); void display(void); }; // member function definitions void circle::store(double r) { radius = r; } double circle::area(void) return 3.14*radius*radius; void circle::display(void) cout << “r = “ << radius << endl; int main(void) { circle c; // an object of circle class c.store(5.0); cout << "The area of circle c is " << c.area() << endl; c.display(); }