Classes:

Classes:

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 youve

seen with Java class Rect { int length; int width; int area; // default private different from structs 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; area = length * width; int main() { } //setLen Rect r; // constructor happens r.setLen(2); int getLen() { //Getters (do the same for width cout << r.getLen() << endl; return length; cout << r.getArea() << endl; } //getLen return 0; } //main int getArea() { return area; } //getArea }; //Rect Classes: method 2 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; int main() { area = length * width; Rect r(3,4); // constructor happens } r.setLen(2); void Rect::setLen(int x) { //add setWidth cout << r.getLen() << endl; length = x; cout << r.getArea() << endl; area = length * width; return 0; } } //main int Rect:: getLen() { // add getWidth return length; } int Rect::getArea() { return area; } Classes: using header files: In header file (Rect.hpp) In separate file (Rect.cpp) In main file (MainProg.cpp) 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 #include "Rect.hpp" #include #include #include Rect::Rect(int x, int y) { length = x; width = y; area = length * width; } void Rect::setLen(int x) { length = x; area = length * width; } void Rect::setWid(int x) { width = x; area = length * width; } int Rect:: getLen() { return length; } int Rect::getWid() { return width; } int Rect::getArea() { return area; } #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; area = length * width; } Rect::Rect(int x, int y) { // constructor length = x; width = y; area = length * width; } // 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 theres a pointer in your classs fields, you should most likely write a destructor Automatically called when object goes out of scope You can use E.g., when program terminates, end of loop if created in loop, end of function if not returned, etc. Delete Note: theres a default destructor that takes care of pretty much everything that didnt require a new, but it never hurts to throw it into your class YOU CANNOT delete ANYTHING THAT YOU HAVENT NEWD!!! Even in a destructor 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 #include "Rect.hpp" Rect::Rect() { // constructor length = 6; width = 5; area = length * width; } Rect::Rect(int x) { // constructor length = x; width = 5; area = length * width; } Rect::Rect(int x, int y) { // constructor length = x; width = y; area = length * width; } Rect::~Rect() { cout << "destroying: " << area << endl; //Nothing is needed // this cout just shows when its called // Also called when something goes out of scope } //

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 In Matrix.cpp class Matrix { int len; int wid; int **mat; public: Matrix(int x, int y); ~Matrix(); //destructor // }; //Matrix header #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: 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; return 0; } //main // what do you think? Dynamically allocated objects class StudentInfo { public: string fname; string lname; int id; }; //StudentInfo int main() { StudentInfo *c; C Charlie fname lname id 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 Passing Dynamically allocated class StudentInfo {

objects public: string fname; string lname; int id; }; //StudentInfo void changeStud(StudentInfo *x) { x->fname = "Samantha"; cout << x->fname << endl; } //changeStud C Charlie fname x lname id 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 Dynamically allocated array of class StudentInfo { objects: bob fname public: string fname;

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

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

fname 2 lname grades 9 4 Worse:Dynamically allocated field in a class: class StudentInfo { public: bob fname string fname; string lname; int *grades; }; //StudentInfo int main() { 0 b lname grades StudentInfo *b; fname b = new StudentInfo[3]; 1 b[0].fname = "bob"; b[0].grades = new int;

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

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 9 4 Whats Wrong? 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; area = length * width; } Rect::Rect(int x, int y) { // constructor

length = x; width = y; area = length * width; } Rect::~Rect() { cout << "destroying: " << area << endl; } void Rect::setLen(int x) { length = x; area = length * width; } void Rect::setWid(int x) { width = x; area = length * width; } int Rect::getArea() { return area; main: int main() { /*hint its 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; } Inheritance class Rectangle: public Shape { // Derived class has access to protected methods and fields public: 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 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 yes yes

Derived classes yes yes no Outside classes yes no no Overloading Constructor: In Rectangle.hpp In Rectangle.cpp Running main: class Rect { #include "Rect.hpp" Rect::Rect() { // constructor length = 6; width = 5; area = length * width; } Rect::Rect(int x) { // constructor length = x; width = 4; area = length * width; } Rect::Rect(int x, int y) { // constructor length = x; width = y; area = length * width; }

// #include "Rect.hpp" int length; int width; int area; public: Rect(); Rect(int x); Rect (int x, int y); // ~Rect(); //destructor int getArea(); }; //Rect header Rect::~Rect() { cout << "destroying: " << area << endl; //Nothing is needed // this cout just shows when its called } int Rect::getArea() { return area; } 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; } u 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); }; 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 <

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

} 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 classs 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. Well see more on this shortly

Recently Viewed Presentations

  • Surface Area of Prisms and Cylinders Lesson 9-8

    Surface Area of Prisms and Cylinders Lesson 9-8

    The surface area of a three-dimensional figure is the sum of the areas of its surfaces. Find the area of each surface, and add all the areas together. Example 1 Add up the areas to find surface area. Surface Area...
  • Pharmaceutical Regulatory and Compliance Congress and Best Practices

    Pharmaceutical Regulatory and Compliance Congress and Best Practices

    21 CFR Part 11 Considerations ... Self Imposed Controls required to satisfy the FDA that electronic records and signatures and the legally binding equivalent to paper based records and signatures The regulation was published on March 20, 1997 with an...
  • Elements, Compounds, Mixtures

    Elements, Compounds, Mixtures

    Air is a mixture of elements such as oxygen, argon and nitrogen mixed with compounds such as carbon dioxide, or you can say that air is a mixture of particles. Some of the elements in air go around as single...
  • Deflasking , Remounting and Occlusal Adjustment

    Deflasking , Remounting and Occlusal Adjustment

    Deflasking , Remounting and Occlusal Adjustment Dr.Mohammad Al Sayed 16/3/2008
  • COSO Framework Overview

    COSO Framework Overview

    Provides examples from a variety of entities - public, private, not-for-profit, and government ... Decline suggestion to address risk associated with specific technologies because of the rapid pace of change. Updates are responsive to public comments (continued) ... COSO Framework...
  • Synthesis

    Synthesis

    Synthesis. Goal: Synthesize a computational concept in some underlying languagefrom user intent using some search technique. State of the art: We can synthesize programs of size 10-20.
  • 幻灯片 1 - Cal Poly

    幻灯片 1 - Cal Poly

    And, very important, air contains a lot of energy of compression, so it is like a bomb waiting to go off. It's useful to think of a pressurized volume of air as filled with tiny springs, all compressed . Those...
  • Topics: Instrument Symbols Flow / Pressure measurement Control

    Topics: Instrument Symbols Flow / Pressure measurement Control

    Instrument Symbols Flow / Pressure measurement Control Valve ... (IEEE) International Standards Organization (ISO) American Petroleum Institute (API) API RP550: Installation of Refinery Instruments And Controls Systems 10. ... BEFORE BORING SILVER SOLDER OR WELD AND GRIND FLUSH 1/2 t...