1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.

Slides:



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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Structures Spring 2013Programming and Data Structure1.
C++ Classes & Data Abstraction
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Classes & Objects classes member data and member function access control and data hiding instantiation of objects class constructor and destructor objects.
OOP Using Classes - I. Structures vs. Classes C-style structures –No “interface” If implementation changes, all programs using that struct must change.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Review of C++ Programming Part II Sheng-Fang Huang.
February 27, 2014CS410 – Software Engineering Lecture #8: C++ Basics II 1 The vector Container Type When a vector is created, its elements are initialized.
Chapter 12: Adding Functionality to Your Classes.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Week 3: Classes, constructors, destructors, new operator Operator and function overloading.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Learners Support Publications Classes and Objects.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
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.
Chapter 10 Introduction to Classes
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
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?
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 Introduction to Object Oriented Programming Chapter 10.
Advanced Programming Constants, Declarations, and Definitions Derived Data Types.
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.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 C++ Classes & Object Oriented Programming Overview & Terminology.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
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.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Procedural and Object-Oriented Programming
Class and Objects UNIT II.
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?
CS410 – Software Engineering Lecture #11: C++ Basics IV
Object Oriented Programming
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?
Chapter 5 Classes.
Lecture 4-7 Classes and Objects
Learning Objectives Classes Constructors Principles of OOP
Classes Short Review of Topics already covered Constructor
Classes and Objects.
Object Oriented Programming Using C++
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this

2 C++ Classes: C++ implements objects by extending the idea of structures. We can specify functions and operations associated with a struct as well as data. typedef We no longer have to use typedef to define a new type when it is a struct. The name of a struct is automatically a new type: point struct point { int x,y; };... point w;

3 C++ Structure A structure not only groups data, it also groups operations that can be performed on the data. struct point { int x,y; print(void) void print(void) { cout << "(" << x << "," << y << ")"; } }; int main() { point w; w.x = 2; w.y = 5; w.print(); } ->(2,5) w.print() invokes the print 'member function' of the point structure.

4 C++ Structure C++ limits the visibility of data and functions by allowing public and private parts to a structure. By default all elements of a structure are public. Programs that use variables of this type are allowed to access the data and functions of the structure. w.y = 5; w.print(); w.print();

5 C++ Structure Declarations within the private section of a structure are only visible to the structure itself. struct point { public: void print(void) { cout << "(" << x << "," << y << ")"; }private: int x,y; }; We can no longer access x and y - but we are allowed to print them!

6 C++ Public and Private members: struct point { public: void print(void) { cout << "(" << x << ",” << y << ")”; } init void init(int u, int v) { x x = u; y y = v; } private: int x,y; }; Now the structure is very secure! - no one can alter the data of the structure without using the functions that are supplied by the structure itself:

7 C++ Public and Private members int main() { point w; w.init(2,5); w.print(); } 'good practice Stopping un-authorised access to data is 'good practice' and is one of the benefits of using C++. The keywords public and private can be used many times within a structure. It is usual to put all public members first and private members last.

8 C++ Class class C++ introduces a new keyword: class A class is exactly the same as a struct - except that all members are private unless specified otherwise The exact opposite of a struct Most people use class rather than struct Always specify private and public -do not use the defaults inline by default Functions written within a class or struct are inline by default. inline functions should be small, and those that are defined within a class should be one or two lines at most.

9 Classes Point struct Point { int x,y; //public void print();//public public: void init(int, int); private: int distance; }; Point class Point { int x,y; //private void print();//private public: void init(int, int); private: int distance; };

10 Scope We can define class member functions outside the class definition. They are then no longer inline by default. Only the function prototype needs to be included within the class point class point { public: print(void); void print(void); private: int x,y; }; Class scope: class scope operator :: The class scope operator :: is used to define functions outside the class declaration. point::print(void) void point::print(void) { cout << "(" << x << "," << y << ")"; }

11 C++ Member Functions Member functions can also be overloaded. class point { public: void init(int u, int v) {x = u; y = v}; print(void) void print(void); print(char *s) void print(char *s); private: int x,y; }; point::print(void) void point::print(void) { cout << "(" << x << ",” << y << ")”; } point::print(char *s) void point::print(char *s) { cout << s; print(); }

12 C++ Overloading Member Functions point w; w.init(4,7); w.print(); cout << endl; w.print("point = "); ->(4,7) point = (4,7)

13 C++ Overloading Member Functions Within the second form of the print function, there is a call to the other function print (it has different arguments) print void point::print(char *s) { cout << s; print(); } No scope operator is required. global function printprint is also a class member function If we want to call a global function print, and print is also a class member function, then we use the scope operator on its own - external scope print(void) void print(void) { cout << " The global print function”; } void point::print(char *s) { cout << s; ::print(); }

14 C++ Class in a Class Classes can contain other classes. char c; class Y { public: char c; }; class X { public: char c; Y y; }; int main() { X x; c = 'A'; x.c = 'B'; x.y.c = 'C'; }

15 C++ Static Members of Classes Static members of classes: If a member variable is declared static, there is only one instance of that in the program. A static variable is common to all class variables. class P { public: static char c; }; char P::c = 'W'; int main() { P P x,y; x.c cout << x.c; x.c x.c = 'A'; y.c cout << y.c; }

16 C++ Static Members of Classes same Correct - but mis-leading. x.c and y.c are the same thing. P::c Better to refer to the static member as P::c int main() { P x; P::c P::c = 'A'; P::c cout << P::c; }

17 C++ this self- referential Each class variable has a 'self- referential' pointer associated with it. Functions within a class may use the variable this class X { public: X* my_address(void); }; X* X::my_address(void) { return this; } int main() { X x; cout << x.my_address(); }

18 C++ Objects action- oriented - C programming is action- oriented - we concentrate on using functions. object- oriented - C++ programming is object- oriented - we concentrate on using classes. instance of a built-in type An instance of a built-in type is called a variable int j; //j is a variable C++ classes are user-defined types an instance of a class we call an instance of a class an object.