Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Copyright © 2002 Pearson Education, Inc. Slide 1.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
Specification and Implementation Separating the specification from implementation makes it easier to modify programs. Changes in the class’s implementation.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 22 - C++ Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Object Oriented Programming Key Features of OO Approach Data encapsulation –data and methods are contained in a single unit, object –promotes internal.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Separating Definition & Implementation Headers and Comments.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Recap: Interfaces A class is a logical abstraction, but an object has physical existence. access-specifier can be: public: Allow functions or data to be.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
CITA 342 Section 1 Object Oriented Programming (OOP)
TEMPLATESTEMPLATES BCAS,Bapatla B.mohini devi. Templates Outline 22.1Introduction 22.2Class Templates 22.3Class Templates and Non-type Parameters 22.4Templates.
Friend Function. 2 Any data which is declared private inside a class is not accessible from outside the class. A non-member function cannot have an access.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
OOP Basics Classes & Methods (c) IDMS/SQL News
 2000 Deitel & Associates, Inc. All rights reserved. 12.1Introduction Templates - easily create a large range of related functions or classes –function.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 2 Objects and Classes
Chapter 12 Classes and Abstraction
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
Chapter 22 - C++ Templates
Classes C++ representation of an object
Templates C++ template
Abstract Data Types Programmer-created data types that specify
Object-Oriented Design (OOD) and C++
A First C++ Class – a Circle
Table of Contents Class Objects.
Chapter 7: Introduction to Classes and Objects
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Anatomy of a class Part I
Introduction to Structured Data Types and Classes
Separate Compilation and Namespaces
Classes: A Deeper Look Outline
Chapter 9 Classes: A Deeper Look, Part 1
Code Organization Classes
Comments, Prototypes, Headers & Multiple Source Files
Classes.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Classes C++ representation of an object
CPS120: Introduction to Computer Science
Chapter 22 - C++ Templates
Templates C++ template
Chapter 22 - C++ Templates
Code Organization Classes
Lecture 8 Object Oriented Programming (OOP)
More C++ Classes Systems Programming.
Anatomy of a class Part I
Classes and Objects Systems Programming.
Presentation transcript:

Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance

Classes The C++ class is derived form the ‘C’ structure Classes have the same syntax as ‘C’ structures Classes have additional features to provide the OO features – The member of a class can be functions as well as data – The member can be public, private or protected (used in inherited classes) A class definition is a template for creating objects A class’s name becomes a data-type and is used in the same way as a data type.

Classes General format class myclass { public: Public members go here. These are normally functions, but can be data. private: Private member go here. These are normally data, but can be functions }; // End of class definition- note the semi-colon

Classes A class is normally split into two files – A header file (.h) that contains the definition of the class and the declaration of the member functions – A C++ source file (.cpp) that contains the definitions of the member functions. Alternatives – All function definitions embedded within the class definition. Now no need for separate header file. - S ometimes done for templates! – classes placed within another C++ source file. Not normally good practice.

Class definition - header file #ifndef myclass_h #define myclass_h class myclass { public: void set_height( float h); void set_age(int a); int get_age(); float get_height(); private: int age; float height; }; #endif The class definition is place in file myclass.h To prevent class re-definition the preprocessor directives #ifndef and #endif are used. The code for the functions (also called methods) can be placed directly within the class definition. It is normally preferred to only declare the functions in the class and to place the function definitions in a separate file.

Class member functions - cpp file The file myclass.cpp containing the function definitions. Note: The function names are preceded with the class name to which the function belongs and the scope resolution operator :: The class definition file is included. Note the header file name is in double quotes. This is the way C++ separates interface (the header file) from implementation (the cpp file). #include “myclass.h” void myclass::set_height (float h) { height = h; } float void myclass::get_height ( ) { return(height); } void myclass::set_age (int a) { age = a; } int myclass::get_age( ) { return age; };

Creating objects An object is an instance of a class.

Creating objects If there is a class called myclass. To create a myclass object in C++ :- myclass billy; // billy is the name of the myclass object to apply the class methods (i.e. member functions) to an object we use the name of the object, a period and the function name and any arguments that the function requires. billy.set_age(22); int x; x = billy.get_age( );

Alternatively using new and pointers myclass * p = new myclass; p->set_age(22); int x; x = p-> get_age( ); * indicates declaration of a pointer -> is the indirection operator which is used with a pointer to access the item that the pointer is referencing.