1 C++ Structures Starting to think about 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

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Class and Objects.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 16: Classes and Data Abstraction Outline 16.1Introduction.
Introduction to Classes and Data Abstraction
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
Review of C++ Programming Part II Sheng-Fang Huang.
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
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.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
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 in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Week 02 Object Oriented Analysis and Designing. Constructors Constructors are member functions of any class, which are invoked the moment an instance.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
1 Review: C++ class 2 kinds of class members: data members and function members Class members are private by default Data members are generally private.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
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.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
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 C++ Classes & Object Oriented Programming Overview & Terminology.
Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects.  Often the objects are modeled after real- world.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2552 Dr. Këpuska Summer 2004 from Dr. S. Kozaitis Spring 2003 slides 1 Summary of Chapter 6: Classes.
Procedural and Object-Oriented Programming
Chapter 16: Classes and Data Abstraction
Review: Two Programming Paradigms
Introduction to Classes
Concepts and Basics of C++ Programming
Object-Oriented Programming
Chapter 5 Classes.
This technique is Called “Divide and Conquer”.
Concepts and Basics of C++ Programming
C++ Classes & Object Oriented Programming
Object Oriented Analysis and Design
Structures in C++.
Introduction to Classes
Classes: A Deeper Look Outline
Starting to think about objects...
CLASSES AND OBJECTS.
COP 3330 Object-oriented Programming in C++
Introduction to Classes and Objects
Starting to think about objects...
Types of Computer Languages
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
Presentation transcript:

1 C++ Structures Starting to think about objects...

Structure  A Structure is a container, it can hold a bunch of things. –These things can be of any type.  Structures are used to organize related data (variables) into a nice neat package. 2

Example - Student Record  Student Record: –Name a string –HW Gradesan array of 3 doubles –Test Gradesan array of 2 doubles –Final Averagea double 3

Structure Members  Each thing in a structure is called member.  Each member has a name, a type and a value.  Names follow the rules for variable names.  Types can be any defined type. 4

5 Example Structure Definition struct StudentRecord { char *name;// student name double hw[3];// homework grades double test[2];// test grades double ave;// final average };

Using a struct  By defining a structure you create a new data type.  Once a struct is defined, you can create variables of the new type. StudentRecord stu; 6

Accessing Members  You can treat the members of a struct just like variables.  You need to use the member access operator '.' (pronounced "dot"): cout << stu.name << endl; stu.hw[2] = 82.3; stu.ave = total/100; 7

8 Structure Assignment  You can use structures just like variables: StudentRecord s1,s2; s1.name = "Joe Student"; … s2 = s1; Copies the entire structure

9 Be Careful  If a member is a pointer, copying means copying the pointer (not what is pointed to). name ave test hw "Joe Student"

10 Probably not what you want StudentRecord s1,s2; s1.name = "Joe Student"; … s2 = s1; s2.name = "Jane Doe"; // now s1.name and s2.name are both // "Jane Doe"

11 Pointers to Structures  Pointers to structures are used often.  There is another member access operator used with pointers: -> StudentRecord *sptr; … cout name; cout ave; it looks like a "pointer"!

12 Sample Function (won't work!) void update_average( StudentRecord stu) { double tot=0; for (int i=0;i<3;i++) tot += stu.hw[i]; for (int i=0;i<3;i++) tot += stu.test[i]; stu.ave = tot/5; }

13 This one works void update_average( StudentRecord *stu) { double tot=0; for (int i=0;i<3;i++) tot += stu->hw[i]; for (int i=0;i<3;i++) tot += stu->test[i]; stu->ave = tot/5; }

14 Or use a reference parameter void update_average( StudentRecord &stu) { double tot=0; for (int i=0;i<3;i++) tot += stu.hw[i]; for (int i=0;i<3;i++) tot += stu.test[i]; stu.ave = tot/5; }

15 Other stuff you can do with a struct  You can also associate special functions with a structure (called member functions).  A C++ class is very similar to a structure, we will focus on classes. –Classes can have (data) members –Classes can have member functions. –Classes can also hide some of the members (functions and data).

16 Quick Example struct StudentRecord { char *name; // student name char *name; // student name double hw[3]; // homework grades double hw[3]; // homework grades double test[2]; // test grades double test[2]; // test grades double ave; // final average double ave; // final average void print_ave() { void print_ave() { cout << "Name: " << name << endl; cout << "Name: " << name << endl; cout << "Average: " << ave << endl; cout << "Average: " << ave << endl; }};

17 Using the member function doubleStudentRecord stu; … // set values in the structure stu.print_ave();

C++ Classes & Object Oriented Programming Exerted from : elearning.najah.edu/OldData/pdfs/C++%20Classes%20Tutorials.ppt

Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects.  Often the objects are modeled after real- world entities.  Very different approach than function-based programming (like C).

Object Oriented Programming  Object-oriented programming (OOP) –Encapsulates data (attributes) and functions (behavior) into packages called classes.  So, Classes are user-defined (programmer- defined) types. –Data (data members) –Functions (member functions or methods)  In other words, they are structures + functions

Classes in C++  A class definition begins with the keyword class.  The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. }; methods Class body (data member + methods) Any valid identifier

Classes in C++  Within the body, the keywords private: and public: specify the access level of the members of the class. –the default is private.  Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.

Classes in C++ class class_name { private: … public: … }; Public members or methods private members or methods

Classes in C++  Member access specifiers –public:  can be accessed outside the class directly. –The public stuff is the interface. –private:  Accessible only to member functions of class  Private members and methods are for internal use only.

Class Example  This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)

Creating an object of a Class  Declaring a variable of a class type creates an object. You can have many variables of the same type (class). –Instantiation  Once an object of a certain class is instantiated, a new memory location is created for it to store its data members and code  You can instantiate many objects from a class type. –Ex) Circle c; Circle *c;

Special Member Functions  Constructor: –Public function member –called when a new object is created (instantiated). –Initialize data members. –Same name as class –No return type –Several constructors  Function overloading

Special Member Functions class Circle { private: double radius; public: Circle(); Circle(int r); void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; Constructor with no argument Constructor with one argument

Implementing class methods  Class implementation: writing the code of class methods.  There are two ways: 1.Member functions defined outside class  Using Binary scope resolution operator ( :: )  “Ties” member name to class name  Uniquely identify functions of particular class  Different classes can have member functions with same name –Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ …}

Implementing class methods 2.Member functions defined inside class –Do not need scope resolution operator, class name; class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Defined inside class

class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } Defined outside class

Accessing Class Members  Operators to access class members –Identical to those for struct s –Dot member selection operator (. )  Object  Reference to object –Arrow member selection operator ( -> )  Pointers

class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } void main() { Circle c1,c2(7); cout<<“The area of c1:” <<c1.getArea()<<“\n”; //c1.raduis = 5;//syntax error c1.setRadius(5); cout<<“The circumference of c1:” << c1.getCircumference()<<“\n”; cout<<“The Diameter of c2:” <<c2.getDiameter()<<“\n”; } The first constructor is called The second constructor is called Since radius is a private class data member

class Circle { private: double radius; public: Circle() { radius = 0.0;} Circle(int r); void setRadius(double r){radius = r;} double getDiameter(){ return radius *2;} double getArea(); double getCircumference(); }; Circle::Circle(int r) { radius = r; } double Circle::getArea() { return radius * radius * (22.0/7); } double Circle:: getCircumference() { return 2 * radius * (22.0/7); } void main() { Circle c(7); Circle *cp1 = &c; Circle *cp2 = new Circle(7); cout<<“The are of cp2:” getArea(); }

Destructors  Destructors –Special member function –Same name as class  Preceded with tilde ( ~ ) –No arguments –No return value –Cannot be overloaded –Before system reclaims object’s memory  Reuse memory for new objects  Mainly used to de-allocate dynamic memory locations

Another class Example  This class shows how to handle time parts. class Time { private: int *hour,*minute,*second; public: Time(); Time(int h,int m,int s); void printTime(); void setTime(int h,int m,int s); int getHour(){return *hour;} int getMinute(){return *minute;} int getSecond(){return *second;} void setHour(int h){*hour = h;} void setMinute(int m){*minute = m;} void setSecond(int s){*second = s;} ~Time(); }; Destructor

Time::Time() { hour = new int; minute = new int; second = new int; *hour = *minute = *second = 0; } Time::Time(int h,int m,int s) { hour = new int; minute = new int; second = new int; *hour = h; *minute = m; *second = s; } void Time::setTime(int h,int m,int s) { *hour = h; *minute = m; *second = s; } Dynamic locations should be allocated to pointers first

void Time::printTime() { cout<<"The time is : ("<<*hour<<":"<<*minute<<":"<<*second<<")" <<endl; } Time::~Time() { delete hour; delete minute;delete second; } void main() { Time *t; t= new Time(3,55,54); t->printTime(); t->setHour(7); t->setMinute(17); t->setSecond(43); t->printTime(); delete t; } Output: The time is : (3:55:54) The time is : (7:17:43) Press any key to continue Destructor: used here to de- allocate memory locations When executed, the destructor is called

Reasons for OOP 1.Simplify programming 2.Interfaces  Information hiding: –Implementation details hidden within classes themselves 3.Software reuse  Class objects included as members of other classes

 for(i=0;i<3;i++)  {  {  for(j=0;j<3;j++)  {  {  for(m=0;m<3;m++)  {  X[i][j][m]=A[i][m]*B[m][j];  C[i][j]+=X[i][j][m];  }

Class Activity  Write a program to computer an area of a rectangle and triangle using the class concept?

Project

How ? 