Classes: Arrays group many objects of the same type (in order)

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

Exercises on Basic OOP TCP1201: 2013/2014. Catch the Bug 1 class Point { private : int x, y; public : Point(int u, int v) : x(u), y(v) { } int getX()
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
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.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Inheritance One of the most powerful features of C++
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object-Oriented Programming in C++ More examples of Association.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Week 2. Functions: int max3(int num1, int num2, int num3) {int result; result = max(max(num1,num2),num3); return result; } //max3.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 16: Introduction to C++
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Arrays  an array of 5 ints is filled with 3,2,4,1,7 int a[5] = {3,2,4,1,7}; // note squiggly brackets  an array of 3 floats is filled with 3.2,1.4,0;
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
1 Object-Oriented Programming Using C++ CLASS 2 Honors.
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.
1 Ugly Realities The Dark Side of C++ Chapter 12.
Yan Shi CS/SE 2630 Lecture Notes
Hank Childs, University of Oregon
Pointer to an Object Can define a pointer to an object:
Andrew(amwallis) Classes!
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Overview 4 major memory segments Key differences from Java stack
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Data Types Programmer-created data types that specify
Advanced Program Design with C++
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Review: Two Programming Paradigms
classes and objects review
Introduction to Classes
Chapter 5 Classes.
Presented By: Nazia Hossain Lecturer, Stamford University
This pointer, Dynamic memory allocation, Constructors and Destructor
group work #hifiTeam
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Overview 4 major memory segments Key differences from Java stack
Chapter 15 Pointers, Dynamic Data, and Reference Types
Chapter 15 Pointers, Dynamic Data, and Reference Types
Given the code to the left:
Classes, Constructors, etc., in C++
Today’s topics UML Diagramming review of terms
What about multi-dimensional arrays?
Arrays an array of 5 ints is filled with 3,2,4,1,7
9-10 Classes: A Deeper Look.
Introduction to Classes and Objects
Pointers and References
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Lecture 8 Object Oriented Programming (OOP)
9-10 Classes: A Deeper Look.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

Classes: Arrays group many objects of the same type (in order) Classes group objects of different types Classes often hold different types together (that logically belong together) Class: a template Includes functions (methods) and variables (fields/properties) associated with the template In essence, a type definition for complex types You get to pick the type Object: An instance of a class; it contains real values instead of variables

Classes: Method 1 Closest to what you’ve seen with Java class Rect { int length; // default – private – different from structs int width; int area; public: Rect() { // this is a constructor length = 3; width = 4; area = length * width; } //Constructor void setLen(int x) { // Setters: why do I need this? (do the same for width – why not area?) length = x; } //setLen int getLen() { //Getters (do the same for width return length; } //getLen int getArea() { return area; } //getArea }; //Rect int main() { Rect r; // constructor happens r.setLen(2); cout << r.getLen() << endl; cout << r.getArea() << endl; return 0; } //main

class Rect { int length; int width; int area; public: Rect (int x, int y); //Just method declarations void setLen(int x); int getLen(); int getArea(); }; //Rect header Rect::Rect(int x, int y) { //Method definitions length = x; width = y; area = length * width; } void Rect::setLen(int x) { //add setWidth int Rect:: getLen() { // add getWidth return length; int Rect::getArea() { return area; Classes: method 2 int main() { Rect r(3,4); // constructor happens r.setLen(2); cout << r.getLen() << endl; cout << r.getArea() << endl; return 0; } //main

Classes: using header files: In header file (Rect.hpp) class Rect { int length; int width; int area; public: Rect (int x, int y); void setLen(int x); void setWid(int x); int getLen(); int getWid(); int getArea(); }; //Rect header In separate file (Rect.cpp) #include "Rect.hpp" Rect::Rect(int x, int y) { length = x; width = y; area = length * width; } void Rect::setLen(int x) { void Rect::setWid(int x) { width = x; int Rect:: getLen() { return length; int Rect::getWid() { return width; int Rect::getArea() { return area; In main file (MainProg.cpp) #include <iostream> #include <stdlib.h> #include <string> #include "Rect.hpp" using namespace std; int main() { Rect r(3,4); // constructor happens r.setLen(2); r.setWid(3); cout << r.getLen() << endl; cout << r.getArea() << endl; return 0; } //main

Constructors In Rectangle.hpp class Rect { int length; int width; int area; public: Rect(); Rect(int x); Rect (int x, int y); //… }; In Rectangle.cpp Rect::Rect() { // constructor length = 6; width = 5; area = length * width; } Rect::Rect(int x) { // constructor length = x; width = x; Rect::Rect(int x, int y) { // constructor width = y; //… int main() { Rect testrect(3,4); cout << testrect.getArea() << endl; Rect testr2(7); cout << testr2.getArea() << endl; Rect testr3; cout << testr3.getArea() << endl; return 0; }

Destructor: If there’s a pointer in your class’s fields, you should most likely write a destructor Automatically called when object goes “out of scope” E.g., when program terminates, end of loop if created in loop, end of function if not returned, etc. You can use Delete Note: there’s a default destructor that takes care of pretty much everything that didn’t require a new, but it never hurts to throw it into your class YOU CANNOT delete ANYTHING THAT YOU HAVEN’T NEW’D!!! Even in a destructor…

Destructor: In Rectangle.hpp In Rectangle.cpp Running main: class Rect { int length; int width; int area; public: Rect(); Rect(int x); Rect (int x, int y); ~Rect(); //destructor //… int getArea(); }; //Rect header In Rectangle.cpp #include "Rect.hpp" Rect::Rect() { // constructor length = 6; width = 5; area = length * width; } Rect::Rect(int x) { // constructor length = x; Rect::Rect(int x, int y) { // constructor width = y; Rect::~Rect() { cout << "destroying: " << area << endl; //Nothing is needed // this cout just shows when it’s called // Also called when something goes out of scope //… int Rect::getArea() { return area; Running main: int main() { Rect r(3,4); // constructor happens Rect r2; Rect r3(4); cout << r.getArea() << endl; cout << r2.getArea() << endl; cout << r3.getArea() << endl; return 0; } //main Output: 12 30 20 destroying: 20 destroying: 30 destroying: 12

Destructor: More useful example: In Matrix.hpp class Matrix { int len; int wid; int **mat; public: Matrix(int x, int y); ~Matrix(); //destructor //… }; //Matrix header In Matrix.cpp #include “Matrix.hpp" Matrix::Matrix(int x, int y) { // constructor len = x; wid = y; mat = new int *[x]; for (int i = 0; i < x; i++) { mat[i] = new int[y]; for (int j = 0; j < y; j++) { mat[i][j] = 0; }//for } // for ) // constructor Matrix::~Matrix() { //needed to prevent memory leak for (int i = 0; i < len; i++) { delete [] mat[i]; } delete [] mat; cout << “matrix destroyed!!“<< endl; //… Running main: int main() { Matrix m(3,4); // constructor happens return 0; } //main Output: matrix destroyed!!

Pointer dot (->) In classes, we use the -> instead of a . when we have the address of an object: class Student { public: string name; int grade; Student (string a, int b); }; //Student header … Student *t = new Student(“Sam”,96); // here t holds the ADDRESS of the student object cout << t->grade ; cout << t->name; The variable is a pointer to the object, not something of type object i.e., Rect *r = new Rect(4,5); // again, r holds the ADDRESS of a Rect object r->getLen();

What do you think? Class StudentInfo { public: }; //StudentInfo string fname; string lname; int id; }; //StudentInfo void changeStud(StudentInfo x) { x.fname = "Samantha"; } //changeStud int main() { StudentInfo Sam(); Sam.fname = "Sammy"; Sam.lname = "Stone"; Sam.id = 3241; changeStud(Sam); cout << Sam.fname << endl; // what do you think? return 0; } //main

Call by Pointer class StudentInfo { public: string fname; string lname; int id; }; //StudentInfo void changeStud(StudentInfo *x) { x->fname = "Samantha"; //-> pointer dot (the pointer version of the . ) // because x holds the address of a StudentInfo } //changeStud int main() { StudentInfo Sam(); Sam.fname = "Sammy"; Sam.lname = "Stone"; Sam.id = 3241; changeStud(&Sam); cout << Sam.fname << endl; // what do you think? return 0; } //main

Dynamically allocated objects class StudentInfo { public: string fname; string lname; int id; }; //StudentInfo int main() { StudentInfo *c; c = new StudentInfo(); c->fname = "Charlie"; //again, pointer dot - c is the address of a StudentInfo Object cout << c->fname << endl; // go to the address found in c and find the fname field. }//main C Charlie fname lname id

Passing Dynamically allocated objects class StudentInfo { public: string fname; string lname; int id; }; //StudentInfo void changeStud(StudentInfo *x) { x->fname = "Samantha"; cout << x->fname << endl; } //changeStud int main() { StudentInfo *c c = new StudentInfo(); c->fname = "Charlie"; changeStud(c); //passing in the address of c cout << c->fname << endl; // go to the address found in c and find the fname field. }//main C Charlie fname x lname id

Dynamically allocated array of objects: bob class StudentInfo { public: string fname; string lname; int id; }; //StudentInfo int main() { StudentInfo *b; b = new StudentInfo[3]; b[0].fname = "bob"; // why don’t we have to say b[0]->fname? Because… // arrays are ALWAYS addresses of the first value of the array // no matter how an array is created cout << b[0].fname << endl; }//main Same for arrays of class objects… fname lname b id fname 1 lname id fname 2 lname id

Dynamically allocated array in an object: bob class StudentInfo { public: string fname; string lname; int *grades; }; //StudentInfo int main() { StudentInfo *b; b = new StudentInfo[3]; //b holds the address of the first //StudentInfo Object in memory – //at that location there’s space //for 3 StudentInfo Objects b[0].fname = "bob"; cout << b[0].fname << endl; b[0].grades = new int[4]; // grades now holds the address //of an array of 4 ints b[0].grades[0] = 94; }//main fname lname b 94 grades fname 1 lname grades fname 2 lname grades

Worse:Dynamically allocated field in a class: class StudentInfo { public: string fname; string lname; int *grades; }; //StudentInfo int main() { StudentInfo *b; b = new StudentInfo[3]; b[0].fname = "bob"; b[0].grades = new int; *(b[0].grades) = 94; // go to the address in b, find the 0th struct, then // find the grades field, and go to the address in the grades field. cout << *(b[0].grades) << endl; }//main bob fname lname b 94 grades fname 1 lname grades fname 2 lname grades

Again: Dynamically allocated field in a class: class StudentInfo { public: string fname; string lname; int *grades; }; //StudentInfo int main() { StudentInfo *b; b = new StudentInfo; b->fname = "bob"; b->grades = new int; *(b->grades) = 94; // go to the address in b, find the grades field, and // go to the address in the grades field. cout << *(b->grades) << endl; }//main bob fname b lname 94 grades

What’s Wrong? int main() { /*hint – it’s in here */ Rect *r; In Rectangle.cpp #include "Rect.hpp" Rect::Rect() { // constructor length = 6; width = 5; area = length * width; } Rect::Rect(int x) { // constructor length = x; width = 4; Rect::Rect(int x, int y) { // constructor width = y; Rect::~Rect() { cout << "destroying: " << area << endl; void Rect::setLen(int x) { void Rect::setWid(int x) { width = x; int Rect::getArea() { return area; In Rectangle.hpp class Rect { int length; int width; int area; public: Rect(); Rect(int x); Rect (int x, int y); ~Rect(); //destructor //… int getArea(); }; //Rect header main: int main() { /*hint – it’s in here */ Rect *r; r->setLen(5); r->setWid(8); cout << r->getArea() << endl; delete r; return 0; } //main

Structs Structs group objects of different types Structs are very similar to classes Struct properties are, by default, public. Structs often hold different types together (that logically belong together) Example: struct StudentInfo { string fname; string lname; int id; StudentInfo next; }; //StudentInfo int main() { StudentInfo Sam; // could also say struct StudentInfo Sam; Sam.fname = "Sammy"; Sam.lname = "Stone"; Sam.id = 3241; StudentInfo studarr[5]; // What did I just do here? studarr[0].fname = "Taylor"; cout << studarr[0].fname << endl; return 0; } //main

class Shape { // Base class public: void setWidth(int w); void setHeight(int h); protected: int width; int height; }; void Shape::setWidth(int w) { width = w; } void Shape::setHeight(int h) { height = h; class Rectangle: public Shape { // Derived class – has access to protected methods and fields int getArea(); int Rectangle::getArea() { return (width * height); int main(void) { Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); cout << "Total area: " << Rect.getArea() << endl; // Print the area of the object. return 0; Note: when using .hpp files (which you should!!!) you must #include “shape.hpp” in Rectangle.hpp Inheritance

Access Control: Access Control and Inheritance: derived class: can access public and protected of base class. Cannot access private (fields, methods) of base class Just like Java Access public protected private Same class yes Derived classes no Outside classes

Overloading Constructor: In Rectangle.hpp class Rect { int length; int width; int area; public: Rect(); Rect(int x); Rect (int x, int y); //… ~Rect(); //destructor int getArea(); }; //Rect header In Rectangle.cpp #include "Rect.hpp" Rect::Rect() { // constructor length = 6; width = 5; area = length * width; } Rect::Rect(int x) { // constructor length = x; width = 4; Rect::Rect(int x, int y) { // constructor width = y; //… Rect::~Rect() { cout << "destroying: " << area << endl; //Nothing is needed // this cout just shows when it’s called int Rect::getArea() { return area; Running main: #include "Rect.hpp" int main() { Rect r(3,4); // constructor happens Rect r2; Rect r3(4); cout << r.getArea() << endl; cout << r2.getArea() << endl; cout << r3.getArea() << endl; return 0; } //main

We can also overload operators! ( e.g., +, =, etc.) In Box.hpp class Box { int length; int width; int height; public: Box (int l, int b, int h); int getVolume(); }; In Box.cpp //constructor Box::Box(int l, int b, int h) { length = l; width = b; height = h; } int Box::getVolume() { return length * width * height; int main() { Box box1(3, 2, 1); Box box2(8, 6, 2); return 0; } You want to “add” two boxes together. Exactly what do you want to add?

We can also overload operators! ( e.g., +, =, etc.) In Box.hpp class Box { int length; int width; int height; public: Box (int l, int b, int h); int getVolume(); Box operator+(Box b); }; In Box.cpp Box::Box(int l, int b, int h) { length = l; width = b; height = h; } int Box::getVolume() { return length * width * height; Box Box::operator+(Box b) { //Overload: add 2 Box objects, // return type is Box Box newbox; newbox.length = length + b.length; newbox.width = width + b.width; newbox.height = height + b.height; return newbox; int main(void) { Box box1(3, 2, 1); Box box2(8, 6, 2); Box box3 = box1 + box2; int volume = box3.getVolume(); cout << "Vol. of box3 : " << volume <<endl; return 0; } Output: Vol. of box3 : 264

==? int Box::getHeight() { return height; } In Box.cpp int Box::getHeight() { return height; } void Box::setHeight(int x) { height = x; Int Box::getVolume() { return length * width * height; Box::Box(int l, int b, int h) { cout <<"Constructor called." << endl; length = l; width = b; height = h; Box Box::operator+(Box b) { Box newbox; newbox.length = length + b.length; newbox.width = width+ b.width; newbox.height = height + b.height; return newbox; bool Box::operator ==(Box b) { return (length ==b.length) && (width == b.width) && (height == b.height); In Box.hpp class Box { int length; int width; int height; public: Box(int l=2, int b=2, int h=2); int getVolume(); Box operator+(Box b); void setHeight(int x); double getHeight(); bool operator==(Box b); }; int main( ) { Box box1(3, 2, 1); Box box2(8, 6, 2); Box box4(3,1,2); if (box1==box4)) { cout << "boxes are equal " << endl; } else { cout << "boxes are not equal" << endl; return 0;

Some other operators you may want to overload: + - * / % ^ & | == != && || = ++ -- += -= etc.

friend Friends are functions and classes declared with the friend keyword. Friends have access to a class’s private fields and methods Note: the class decides what other classes, methods, and functions can be friends with that class. So the friend declaration happens inside the class. We’ll see more on this shortly