Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Object-Oriented Programming Using C++ CLASS 27.
Chapter 8 More Object Concepts
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes.
CS 210 DATA STRUCTURES AND ALGORITHIMS Fall 2006.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
CITA 342 Section 1 Object Oriented Programming (OOP)
Classes, Interfaces and Packages
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Seventh step for Learning C++ Programming CLASS Declaration Public & Private Constructor & Destructor This pointer Inheritance.
Chapter 11: Abstract Data Types Lecture # 17. Chapter 11 Topics The Concept of Abstraction Advantages of Abstract Data Types Design Issues for Abstract.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Inheritance and Polymorphism
Pointer to an Object Can define a pointer to an object:
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Procedural and Object-Oriented Programming
Modern Programming Tools And Techniques-I
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
7. Inheritance and Polymorphism
Final and Abstract Classes
Review: Two Programming Paradigms
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Road Map Introduction to object oriented programming. Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
UML Class Diagram: class Rectangle
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
group work #hifiTeam
Lecture 9 Concepts of Programming Languages
CS212: Object Oriented Analysis and Design
Introduction to Classes
Inheritance Basics Programming with Inheritance
Computer Programming with JAVA
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Introduction to Classes and Objects
CMSC202 Computer Science II for Majors Lecture 10 and 11 – Inheritance
Object-Oriented PHP (1)
CPS120: Introduction to Computer Science
Final and Abstract Classes
CS148 Introduction to Programming II
More C++ Classes Systems Programming.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)

Classes in C++ A class is a means of abstraction in C++ A class is a specification of a group of objects that all have the same essential properties A variable of class type is known as object and the operations on that type are called methods

Difference b/w Structures and Classes A structure is a collection of named fields whereas a class is a collection of named fields and methods that apply to objects of that class type. Structures do not support data encapsulation whereas classes do, restricting access to members of a class to methods of a class itself FOR EXAMPLE...

A class example class Student Properties (data members) name, graduation year, list of courses, number of courses Operations (methods) List Courses List Student Information Add a Course Set Graduation Date

A C++ definition of a class class Student// declares a class data type {// does not allocate memory private :// 4 private data members char studentName[30]; string courses[6]; int numCourses; int gradDate; public : // 5 public function members void AddCourse(string9 CourseName); void ListCourses(void) const; void ListInfo(void) const; void SetGradDate(int year); Student(); } ; // Constructor Function

An object is an instance of a class For example: object studentA Name: Andrea Student Graduation Year: 2007 List of Courses: CSCI132, MATH331 Number of courses: 5

Use of C++ data type class Facilitates re-use of C++ code for an ADT. Software that uses the class is called a client. Variables of the class type are called objects or instances of the class. Client code uses public member functions to handle its class objects. Private members cannot be directly accessed by client code. It can only be accessed by member functions.

C++ Data Type class represents an ADT 2 kinds of class members: data members and function members class members are private by default data members are generally private function members are generally declared public private class members can be accessed only by the class member functions, not by client code.

Implementing Methods Methods of a class can be implemented within the declaration of a class or outside it.

Example 1 class Student// declares a class data type {// does not allocate memory private :// 4 private data members char studentName[30]; string courses[6]; int numCourses; int gradDate; public : Student(); // 5 public function members void AddCourse(string CourseName); void ListCourses(void); void ListInfo(void); void SetGradDate(int year) { gradDate = year; } } ;

Example 2 // SPECIFICATION FILE ( student.h ) // Specifies the data and function members. class Student { public:... private:... } ; // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions....

Implementation file for Student // IMPLEMENTATION FILE ( student.cc ) // Implements the Student member functions. #include "student.h" // also must appear in client code #include... void Student :: SetGradDate (int year ) { gradDate = year; }...

Implementing Methods Student :: SetGradDate(int year); { gradDate = year; } In the heading, the function's name is preceded by the class name and :: - otherwise C++ won't realize this is a class’s member function. Within the body of the function, the class’s member variables and other member functions may all be accessed.

#include "student.h" // includes specification of the class #include int main ( void ) { int newYear; string newCourse; Student newStudent;// create new Student object cout << "Enter graduation year: "; cin >> newYear; newStudent.SetGradDate(newYear); // set Graduation date cout << "Enter course to be added: "; cin >> newCourse; newStudent.AddCourse(newCourse); // Add a course newStudent.ListInfo(); // List Student information newStudent.ListCourses(); // List courses } // end of main Client Code Using Student

Inheritance Inheritance: The ability of a class to derive properties from a previously defined class. Inheritance is a relationship among classes.   A class can derive the behavior and structure of another previously defined class. base class derived class   The derived class is also called as subclass, child.   The base class is also called as superclass, parent.

Inheritance cont. In C++, a derived class inherits all the members of its base class, except the constructors and destructor.   A derived class has data members and member functions of the base class in addition to the members it defines.   A derived class can revise any inherited member function.

Constructor Arguments When a constructor does not have to accept arguments, it is called an object’s default constructor. Like regular functions, constructors may accept arguments, have default arguments, be declared inline, and be overloaded.

Destructors A destructor is a member function that is automatically called when an object is destroyed.   Destructors have the same name as the class, preceded by a tilde character (~)   In the same way that a constructor is called then the object is created, the destructor is automatically called when the object is destroyed.   In the same way that a constructor sets things up when an object is created, a destructor performs shutdown procedures when an object is destroyed.

Function Overloading In C++, several functions of the same name can be defined as long as these function name different sets of parameters (different types or different number of parameters).

Object Orientation In recent years, the object oriented way of doing things has become increasingly popular. Object oriented ways of thinking have been applied in many different areas of computer science. E.g.   Software system design   Operating systems   Programming languages

O-O design principles Organise software as a collection of objects and classes. Objects provide a set of operations (methods- behaviour) Objects have an internal state. Objects belong to (are instances of) a class.

Example: class Rectangle { private: float width, length, area; public: void setData(float, float); void calcArea(void); float getWidth(void); float getLength(void); float getArea(void); };

13.3 Defining Member Functions Class member functions are defined similarly to regular functions. void Rectangle::setData(float w, float l) { width = w; length = l; }

13.4 Defining an Instance of a Class Class objects must be defined after the class is declared. Defining a class object is called the instantiation of a class. Rectangle box; // box is an instance of Rectangle

Accessing an Object’s Members box.calcArea();

Pointers to Objects Rectangle *boxPtr; boxPtr = &box; boxPtr->setData(15,12);