Classes C++ representation of an object

Slides:



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

1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Road Map Introduction to object oriented programming. Classes
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
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.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Classes Definition A class is a data type whose variables are objects Object – a variable that has member functions as well the ability to hold.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
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)
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Classes, Interfaces and Packages
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Classes Classes are a major feature of C++. They support – – Abstraction – Data hiding – Encapsulation – Modularity – Re-use through inheritance.
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 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Chapter 12 Classes and Abstraction
Procedural and Object-Oriented Programming
Classes (Part 1) Lecture 3
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Templates C++ template
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
A First C++ Class – a Circle
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
More about OOP and ADTs Classes
Introduction to Classes
Classes and Data Abstraction
Andy Wang Object Oriented Programming in C++ COP 3330
More about OOP and ADTs Classes
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Andy Wang Object Oriented Programming in C++ COP 3330
Classes C++ representation of an object
CPS120: Introduction to Computer Science
Templates C++ template
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
C++ Object Oriented 1.
Presentation transcript:

Classes C++ representation of an object Used to support object oriented programming A class is a user-defined container that provides: Encapsulation of both object data and supported operations An extension of the “struct” type to include operations as well as data Inheritance (ability to derive new classes from existing ones) Polymorphism “overloading” of class methods (functions) and/or built-in C++ operators

Class Definition Syntax Defined with the keyword “class” “public” section Contains information visible to a user of the class “private” section Contains information only visible within the class itself (Note: a third section, called “protected” is used by inherited classes) class className { public: List all public data and functions private: List all private data and functions }; // Don’t forget the semicolon!

Classes: public vs. private Classes store both data and methods (aka. functions) Methods are used to manipulate data Direct data manipulation is discouraged public vs. private sections Access to data/methods in the private section is limited to the class itself Only data/methods in the public section are visible externally Together they facilitate “encapsulation” User is shielded from internal implementation details of the object The object becomes a “black box”

Simple Class Example #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); // default constructor! void setValue(int v) ; int getValue(); private: int value; }; #endif #include “MyClass.h“ MyClass::MyClass() { value=0; } void MyClass::setValue(int v) { value= v; int MyClass::getValue() { return value; MyClass.h MyClass.cpp contains class definition and prototypes contains implementation details Classes are generally defined and implemented in two files

Class Creation Class is defined and implemented in two files ex: MyClass.h and MyClass.cpp Class definition and function/method prototypes placed in header file MyClass.h Class function implementations placed in a “coding” file MyClass.cpp Header file is #include(‘ed) in the .cpp file at compilation

Class Creation (header file) contains class definition public and private sections Data members Prototypes for methods #ifndef compiler directive Prevents multiple definitions of class if header is included more than once #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); // default constructor! void setValue(int v) ; int getValue(); private: int value; }; #endif

Class Implementation (“coding” file) #include the header file Contains implementation details for prototyped methods (functions) Details are hidden from users Class member functions can access all class data members Scope Resolution operator Double colon “::” prefix for each member function Informs the compiler that the function is a member of the class #include “MyClass.h“ MyClass::MyClass() { value=0; } void MyClass::setValue(int v) { value= v; int MyClass::getValue() { return value; Note the use of scope resolution operator

Constructors Special member function for creating new instances of the class MyClass::MyClass() { … }; Important properties! Same name as the class No return value Newly created object instance is “returned” Automatically called upon creation of a new object instance “Default constructor” has no arguments Additional constructors can be created that take arguments (polymorphism)

Usage of Classes and Objects Create a class instance E.g. MyClass c1; Creates an instance of the MyClass object named “c1” Default constructor automatically invoked Allocates space in memory for the new variable (c1) Similar to other data type declarations int value; Create and allocate memory for a variable named “value” of type int

Other Member Functions May be either public or private Three main types: Mutators (modifiers) modify or change internal data c1.setvalue(x) Inspectors (accessors) Retrieve internal data c1.getvalue(x) Facilitators Helper functions (often private) Member functions are accessed using the dot (.) operator Similar to using a struct Data members are also accessible using (.) Member function use is preferred

Access to the Class Class definition/coding file is compiled separately Result is an object file (.o) that contains the complete class implementation Include the header file in any program that wishes to use the object because the class definition is contained in the header file #include “MyClass.h” (Note: use quote marks instead of brackets (< >) around class file) Link the user program together with the object file for the class Facilitates “encapsulation” Class definition is separate from the user program The class is, effectively a new, independent, data type

Compiler Directives Prevents multiple definitions of a class #ifndef tests MYCLASS_H If MYCLASS_H is not defined #define defines it, then… Then, everything up to the #endif is included i.e. the class gets defined If MYCLASS_H is defined skip to the #endif skip over everything between #ifndef and #endif Don’t try to (re)define the class! #ifndef MYCLASS_H #define MYCLASS_H (define the class and prototypes) #endif