Presentation is loading. Please wait.

Presentation is loading. Please wait.

What about multi-dimensional arrays?

Similar presentations


Presentation on theme: "What about multi-dimensional arrays?"— Presentation transcript:

1 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

2 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…

3 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;

4 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

5 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

6 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

7 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

8 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; }

9 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

10 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

11 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!!

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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");

21 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

22 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

23 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

24 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

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

26 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 :

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

28 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.


Download ppt "What about multi-dimensional arrays?"

Similar presentations


Ads by Google