February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.

Slides:



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

1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
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.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Lab Session-XIV CSIT121 Spring 2002 b Namespaces b First Class Travel b Lab Exercise 14 (Demo) b Lab Exercise b Practice Problem.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Introduction to C++. Overview C++? What are references Object orientation Classes Access specifiers Constructor/destructor Interface-implementation separation.
1 Object-Oriented Programming Using C++ CLASS 27.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
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.
Multiple Files. Monolithic vs Modular  one file before  system includes  main driver function  prototypes  function.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
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.
Object-Oriented Programming. Procedural Programming All algorithms in a program are performed with functions and data can be viewed and changed directly.
11 Introduction to Object Oriented Programming (Continued) Cats.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
Practice Building Classes Modeling Objects. Problem Write a program that computes the Dean’s List (full-time students whose GPA 3.0), using a student.
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?
Computing and Statistical Data Analysis Lecture 6 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Introduction to classes and objects:
Object-Oriented Paradigm The Concept  Bundled together in one object  Data Types  Functionality  Encapsulation.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
1 What is a Named Constant? A named constant is a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
1 Data Structures CSCI 132, Spring 2014 Lecture 2 Classes and Abstract Data Types Read Ch Read Style Guide (see course webpage)
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
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.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Object Access m1.write(44); m2.write(m2.read() +1); std::cout
Pointer to an Object Can define a pointer to an object:
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
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Classes Object-oriented programming: Example: Bank transactions
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?
Lecture 4-7 Classes and Objects
Introduction to Classes
Code Organization Classes
Classes C++ representation of an object
Code Organization Classes
Lecture 8 Object Oriented Programming (OOP)
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

February 28, 2005 Introduction to Classes

Object Oriented Programming An object is a software bundle of related variables and methods. Software objects are often used to model real-world objects you find in everyday life. Software objects interact and communicate with each other using messages also called methods. A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind

Object-Oriented Viewpoint An application is a set of objects interacting by sending messages The functionality of an object is described by its methods, it’s data is stored in private variables An object’s functionality can be invoked by sending a message Everything is an object

Classes A simple class #include class Student { string firstName; } ;

A good start but there is a problem #include using namespace std; class Student { string firstName; }; int main() { Student s; s.firstName = "Jack"; cout << “My first name is “ << s.firstName << endl; return 0; }

By default, member variables are private #include using namespace std; class Student { string firstName; }; int main() { Student s; s.firstName = "Jack"; cout << “My first name is “ << s.firstName << endl; return 0; }

We could do this but we shouldn’t #include using namespace std; class Student { public: string firstName; }; int main() { Student s; s.firstName = "Jack"; cout << “My first name is “ << s.firstName << endl; return 0; }

This is better #include using namespace std; class Student { private: string firstName; public: void setFirstName(string name) { firstName = name; } }; int main() { Studennt s; s.setFirstName ("Jack"); cout << "My first name is " << s.getFirstName () << "." << endl; return 0; }

This is better still #include using namespace std; class Student { private: string firstName; public: string getFirstName() { return firstName; } void setFirstName(string name) { firstName = name; } }; int main() { Student s; s.setFirstName ("Jack"); cout << "My first name is " << s.getFirstName () << "." << endl; return 0; }

This is even better #include using namespace std; class Student { private: string firstName; public: string getFirstName(); void setFirstName(string name) }; string Student::getFirstName() { return firstName; } Void Student::setFirstName(string name) { firstName=name; } int main() { Student s; s.setFirstName ("Jack"); cout << "My first name is " << s.getFirstName () << "." << endl; return 0; }

Using Header files To better organize our classes and programs we can put declarations in a header file and implementations in a source file Let’s do this for the Student class To avoid recursively including header file more than once, use this preprocessor directive #ifndef STUDENT_H #define STUDENT_H #endif

Student.h #ifndef STUDENT_H #define STUDENT_H #include using namespace std; class Student { private: string firstName; public: string getFirstName(); void setFirstName(string name); }; #endif

Student.cpp #include “Student.h" #include string Student::getFirstName () { return firstName; } void Student::setFirstName (string name) { firstName = name; }

main.cpp #include #include “Student.h" using namespace std; int main() { Student s; s.setFirstName("Jack"); cout << "My first name is " << s.getFirstName() << "." << endl; return 0; }

Inline Member Functions It is possible to place a member function’s body in the class declaration instead of its prototype. This is often done when a member function has a small body The result of this is that the compiler will replace calls to the function with the actual body of the function. This eliminates the overhead associated with calling a function. This makes the code bigger but can improve performance.

Student.h with inline member function #ifndef STUDENT_H #define STUDENT_H #include using namespace std; class Student { private: string firstName; public: string getFirstName() { return firstName; } void setFirstName(string name); }; #endif

Constructors It would be nice to have a member function that: Initializes a class in anyway deemed necessary by the class writer. Is called automatically whenever an instance of the class is created. There is such a member function and it is called a Constructor.

Constructors Constructors always have the same name as the class. A constructor has no return type, not even void. This is because constructors cannot be explicitly called and cannot return values.

Student Class Header with Constructor #ifndef STUDENT_H #define STUDENT_H #include using namespace std; class Student { private: string firstName; public: Student(); //Constructor string getFirstName() { return firstName; } void setFirstName(string name); }; #endif

Creating an assigning a random ID to each student in a class Let’s do this at the time we create a Student object. First add a private member variable of time int called studentID Inside of Student.h Student(); Inside of Student.cpp Student() { srand(time(NULL)) make sure to include studentTime = rand(); }