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?

Slides:



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

Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Classes & Objects classes member data and member function access control and data hiding instantiation of objects class constructor and destructor objects.
Road Map Introduction to object oriented programming. Classes
Chapter 14: Overloading and Templates
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
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.
Chapter 10 Defining Classes. User-defined Types Are data types defined by programmers. Include: – typedef: simple data definition – struct: a structure.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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 © 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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
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
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
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.
Chapter 11 Friends and Overloaded Operators. Introduction to function equal // Date.h #ifndef _DATE_H_ #define _DATE_H_ class CDate { public: CDate();
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Procedural and Object-Oriented Programming
Classes C++ representation of an object
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?
Abstract Data Types Programmer-created data types that specify
A First C++ Class – a Circle
Review: Two Programming Paradigms
Introduction to Classes
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 Structured Types, Data Abstraction and Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Introduction to Classes
Classes and Objects.
Namespaces How Shall I Name Thee?.
Submitted By : Veenu Saini Lecturer (IT)
Classes C++ representation of an object
CS 144 Advanced C++ Programming February 21 Class Meeting
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

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? what are structures used for? is a structure definition an executable statement? Can it be put in a header file? why includeing a header file with structures definition multiple times is a problem? how is this problem solved? what are #define #ifndef #endif? what is a structure variable? what is the term for elements of a structure? do elements of the same structure (different structures) have to have unique names? how can structures be initialized? what happens when one structure variable is assigned the value of another? can structure variables be directly compared? passed as parameters? by value? by reference? can a function return a structure? can one structure be included in another structure? What is a substructure? can a structure include an array? how can an array of structures be declared?

Classes

What’s Wrong with Structures? structure is an aggregate construct providing encapsulation encapsulation - combining a number of items in a single entity consider implementing date: as structure: struct Date{ int month; int day; int year; }; and a set of functions manipulating dates: void setDate(Date &d, int m, int d, int y); void addYear(Date &d, int n); bool compare(Date &d1, Date &d2); problems: there is no explicit connection between data type (structure) and these functions it does not specify that the functions listed should be the only ones that access and modify date variables if there is a bug in Date manipulation - it can be anywhere in the program if modification of Date is needed - all program needs to be updated

Class Definition class is an aggregate construct class may contain member variables (attributes) and member functions (methods) member variables and member function prototypes are declared within class definition member functions can manipulate member variables without accepting them as parameters class Date { // class name public: // ignore this for now void set(int, int, int); int getDay(); int month; int day; int year; }; // don’t forget the semicolon a variable of type class is called object (how is a variable of type structure called?) Date mybday; the object is said to belong to the class or be an instance of the class each object has member variables and can call member functions of its class values of all member variables is the state of the object addressing the members is done using dot-operator: mybday.set(10, 26, 68); cout << mybday.day;

Public and Private Members public/private attributes control the way the class members are accessed (why do we want to do that?) public member can be accessed within member functions (no scope resolution needed) and outside (with dot operator) private member - can only be accessed within member functions class Date { // class name public: void set(int, int, int); int getday(); private: int month; int day; int year; }; // don’t forget semicolon make member variables private, make functions either public or private. This restricts manipulation of variables to member function which makes debugging and changes in class easier examples: mybday.set(10, 5, 99); // good mybday.year=99; // ERROR - private member

Member Function Definitions functions can be defined either inside or outside class definition outside definition - class name (called type qualifier) and scope resolution operator (::) precedes function name: void Date::set(int m, int d, int y){ month=m; // no dot with member variables day=d; // no declaration of member variables year=y; } to define inside - replace prototype with definition class Date { public: void set(int, int, int); int getDay(){return day;} private: int month; int day; int year; }; // don’t forget semicolon function defined inside is called in-line function for style: use for very small functions. One line is good

Mutators and Accessors accessor function - member function that does not modify the state of an object (only returns the information about the object’s state) mutator function - member function that modifies the state of an object accessors should be marked with const type modifier so that compiler can find accidental object state modification also remember to use const with parameters that are not modified class Date { public: // mutator void set(int, int, int); // mutator int getMonth() const; // accessor int getDay() const {return(day);} // accessor in-line private: int month; int day; int year; }; separate mutators and accessors - a function should not do both since variables are private they all need accessor are we missing an accessor in Date?

Constructors constructor - mutator that is invoked automatically when object is declared used to assign an initial state to the object (initialize object) constructor it has the same name as class constructor does not return a value do not put void as a return value of constructor class Date{ public: Date(int, int, int); // constructor private: int month, day, year; }; outside constructor definition (can also be inlined) Date::Date(int m, int d, int y){ month=m; day=d; year=y; } constructor is invoked at declaration: Date mybday(10,26,99); or directly: yourbday=Date(10,26,00);

Multiple/Void Constructors class can have multiple constructors which constructor to call is determined by number and type of arguments function overloading – same name functions distinguished by number/type of arguments it is bad style to define constructors that differ by type of arguments only - confusing if no constructors declared - object can be defined without initialization void (or default) constructor - a constructor that does not take arguments caveat: if at least one constructor declared - object has to be always initialized (needs a void constructor) class Date { public: Date(int, int, int); // constructor Date(int, int); // another constructor Date(); // void or default constructor private: int month; int day; int year; }; calling default constructor (if constructors defined): Date mybday; if array of objects, default constructor is invoked Date friendsbday[20]; what is this? Date mybday(); It looks like a default constructor call but to the compiler it looks like a function prototype

Constructors for Member Objects (void) constructors for member objects are invoked before constructor for containing object example class one{ public: one() { cout << ”one's constructor" << endl; }; }; class two{ public: two() { cout << ”two's constructor" << endl; }; private: one o; int main() { two ob2; } prints one’s constructor two’s constructor note, if containing object does not have a constructor, member object (void) constructors are still invoked It looks like a default constructor call but to the compiler it looks like a function prototype

Friend Functions to allow non-member function to access private class members it should be declared as friend function: class Date { public: // compares two dates friend bool equal(const Date&, const Date&); private: int month; int day; int year; }; a friend-function is not a class member - it’s definition should not be preceded with type qualifier (Date::) no separate prototype (outside class definition) is necessary for friend equal is invoked as non-member function - without dot operator: if(equal(date1, date2)) { ... friend-functions circumvent information hiding and should be used as rare as possible and with great care only use friend-function if the task requires access to private members of more than one object and the objects are used symmetrically

Static Constants the scope of static constant is the class: it is available to all methods it is stored only once but every object of this class has access to it can be private or public class Date { public: ... private: static const int CENT21 = 2001; }; variables can also be declared static. Careful with those since they are in effect global variables (not as problematic, why?)

Program Layout program with objects is laid out as follows: header - class definition (inline function definitions), global constants and other non-executable constructs related to class program file - member function definitions multiple related classes may be put in one header/program file pair

Classes Review What is class? Why are classes needed? What is object? What is the difference between object and class? What do we mean when we say object belongs to a class? What’s member variable? Member function? Method? Attribute? What’s an in-line function definition? What are these operators used for? “.” “::” What is the difference between public and private members of class? What is the state of an object? What is mutator? Accessor? How is accessor function distinguished? What is constructor? Is constructor mutator or accessor? What is the name of constructor? How many parameters can constructor take? Can there be more than one constructor? What is constructor overloading? What is void constructor? What is the return value of constructor? What is friend function? Why is it needed? What are static attributes?