What about multi-dimensional arrays?

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

Destructors Math 130 Lecture # xx Mo/Da/Yr B Smith: New lecture for 05. Use this for evolving to Java B Smith: New lecture for 05. Use this for evolving.
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.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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 in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
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++
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++
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.
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.
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:
Procedural and Object-Oriented Programming
Learning Objectives Pointers as dada members
Classes C++ representation of an object
Overview 4 major memory segments Key differences from Java stack
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Motivation and Overview
classes and objects review
Introduction to Classes
Chapter 5 Classes.
Subject Name: PROGRAMMING IN C++ Subject Code: 10EC665
Presented By: Nazia Hossain Lecturer, Stamford University
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
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:
Dr. Bhargavi Dept of CS CHRIST
Classes, Constructors, etc., in C++
Today’s topics UML Diagramming review of terms
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Programming and Debugging
Arrays an array of 5 ints is filled with 3,2,4,1,7
Introduction to Classes and Objects
Classes C++ representation of an object
Pointers and References
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Classes: Arrays group many objects of the same type (in order)
Lecture 8 Object Oriented Programming (OOP)
Pointers, Dynamic Data, and Reference Types
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

What about multi-dimensional arrays? int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 4x3 array for (int i = 0; i < 4; i++) { array of x[i] = new int[3]; addresses } //for for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { x[i][j] = i+j; cout << x[i][j]; cout << endl; X 0 1 2 1 2 3 2 3 4 3 4 5

How about deleting? int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 4x3 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for for (int j = 0; j < 3; j++) { x[i][j] = i+j; Why can’t we just do this? delete [] x; // compiles just fine…

Deleting a multi-dimensional array int **x = NULL; // Pointer initialized with null x = new int *[4]; // Allocate memory on heap for a 4x3 array for (int i = 0; i < 4; i++) { x[i] = new int[3]; } //for delete [] x[i]; delete [] x;

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(3,4); // 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 <cctype> #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

Destructor: In Rectangle.hpp In Rectangle.cpp 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 //… 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!!

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? // 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

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 is printed (hint: when is anything printed in this)? In MyClass.hpp: class MyClass { int i; string j; public: MyClass(int x,string s); ~MyClass(); }; In MyClass.cpp MyClass::MyClass(int x,string s) { i = x; j = s; } MyClass::~MyClass() { cout << j; In Main: void f() ; int main() { MyClass t(3,"as"); f(); MyClass t3(2,"m"); return(0); } void f() { MyClass t(2,"a"); for (int i = 0;i < 2; i++) { MyClass t2(3,"l");

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) { width = w; } void setHeight(int h) { height = h; protected: int width; int height; }; class Rectangle: public Shape { // Derived class – has access to protected methods and fields int 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 { double length; double breadth; double height; public: Box (double l, double b, double h); double getVolume(); }; In Box.cpp //constructor Box::Box(double l, double b, double h) { length = l; breadth = b; height = h; } double Box::getVolume() { return length * breadth * height; int main(void) { Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); 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 { double length; double breadth; double height; public: Box (double l, double b, double); double getVolume(); Box operator+(Box b); }; In Box.cpp Box::Box(double l, double b, double h) { length = l; breadth = b; height = h; } double Box::getVolume() { return length * breadth * height; Box Box::operator+(Box b) { //Overload: add 2 Box objects. Box box; box.length = length + b.length; box.breadth = breadth + b.breadth; box.height = height + b.height; return box; int main(void) { Box Box1(3.3, 1.2, 1.5); Box Box2(8.5, 6.0, 2.0); Box Box3 = Box1 + Box2; double volume = Box3.getVolume(); cout << "Vol. of Box3 : " << volume <<endl; return 0; } Output: Vol. of Box3 : 297.36

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.