Introduction to the Web and .NET

Introduction to the Web and .NET

CS3340 OOP and C++ L. Grewe C++ C++ is an object-oriented programming language. Who created C++? Bjarne Stroustrup, while at AT&T. (now, a professor in Computer Science at Texas A&M University) http://www.research.att.com/~bs/ Overview

C++ basics (see course website) C++ and OOP concepts revisited Two ways of code reuse: composition & inheritance Composition (has-a relationship): An object of a class has an object of another class in it. Use predefined code of a class by creating an object of the class. Inheritance (is-a relationship): A child class inherits everything from its parent class. Use predefined code of a class by sub-classing from it. class CWheel { private: float air-pressure; public:

void inflate(); }; // predefined class class CPassengerCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel; public: }; class CCar { private: CEngine engine;

CBreak break; public: void accelerate(); void stop(); }; // predefined class a passenger car is a car a truck is a car a race car is a car class CPassengerCar : public CCar { private: int number-of-seats; CWheel fl-wheel, fr-wheel; CWheel rl-wheel, rr-wheel;

public: }; class CCar { private: CEngine engine; CBreak break; public: void accelerate(); void stop(); }; Grouping data and operations (functions/methods) together to

facilitate code reuse. Controlling the access of the members. (Private members are hidden and can only be accessed by the member functions of the class. Users can only use the given public functions to alter the private data members). class CCar { protected: CEngine engine; CBreak break; public: void accelerate();

void stop(); }; Protected members are hidden and can only be accessed by the member functions of the class and its decedent classes. One method behaves differently for different objects. class CShape { private: int color; public: virtual float area() { }; // a virtual function can be overridden by the children

}; class CCircle : public CShape{ public: float area() {return (pi * r * r)}; }; class CRect : public CShape{ public: float area() {return (w*h)}; }; One function behaves differently for different objects. CShape *shapes[4]; CCircle *c1 = new CCircle(5); CCircle *c2 = new CCircle(10);

CRect *r1 = new CRect(4,5); CRect *r2 = new CRect(8,5); shapes[0] shapes[1] shapes[2] shapes[3] = = = = (CShape (CShape (CShape (CShape

*) *) *) *) c1; // cast to parent allowed c2; // cast to parent allowed r1; // cast to parent allowed r2; // cast to parent allowed for (I=0; I<4; I++) cout << shapes[I]->area() << endl; Interface in C++ Use Abstract Class

There is no Interface in C++. The workaround is to define abstract classes with pure virtual functions. Pure virtual functions: have only function headers (APIs), but no function bodies. virtual return-type function-name (argument list) = 0; Abstract classes: have one or more pure virtual functions; can be used for inheritance by child classes, can not be used for instantiation of objects. The child classes are required to implement all pure virtual functions with predefined APIs. Setting up standard interfaces Polymorphism makes it possible for objects of different child classes to have the same interface. To enforce a standard interface, one can make the parent class

an abstract class and subclass from it. An abstract class is a class containing a pure virtual function. One cant instantiated an abstract class. It is just for setting up standard interfaces. Its child classes must override and implement the pure virtual function. (A regular virtual function is not required to be overridden by the child classes and it is inherited if it is not overridden.) class CShape { public: virtual float area() = 0; // a pure virtual function }; // CShape a-shape; not allowed. class CCircle : public CShape{ public:

float area(); // must be implemented. }; class CRect : public CShape{ public: float area(); // must be implemented. }; Common OOP need Iterators traverse a collection of objects from the beginning to the end. iterators have been implemented for all C++ collection classes (e.g. vector). Have templates (like generics in Java) vector shapes; vector::iterator it = shapes.begin();

while(it != shapes.end()) (*it++)->display(); Coding with an IDE (MS Visual Studio 2XXX) The actual sequence will depend on your version of MS Visual Studio Using Start->Program Files-> MS Visual Studio 2XXX-> MS Visual Studio XXXX Select C++ as default environment if prompted to. File->New->Project-> Other Languages->Visual C++->Win32->Win32 Console Application Name: test Location: My Documents\Visual Studio 2XXX\Projects (fix this to a locationlike your USB fob that you will remember to take with you) Some Review and other things

I/O , file I/O Templates Vector Protection types pointers Console IO Reading Input: cin

console input an object of iostreams only supports one operator >> reads input for its arguments, one at a time auto converts input to the type of the arguments Displaying Output: cout console output an object of iostreams only supports one operator << sends output to the console for its arguments, one at a

time auto converts output of the type of the arguments for display display format can be specified File IO #include #include // file io streams #include // file io streams using namespace std; int main() { string infile, outfile; cout << "Enter input file name: \n"; cin >> infile; cout << "Enter output file name: \n"; cin >> outfile;

ifstream in(infile.data()); // Open for reading ofstream out(outfile.data()); // Open for writing string s; while(getline(in, s)) // Discards newline char out << s << "\n"; // ... must add it back } Protection Type public, private and protected public members are accessible to all. protected members are accessible to derived class. private accessible only to same class.

vector (smart array) To use: #include To define: vector v; To add an element: v.push_back(object); To access an element: v[i] (just like an array) Eaxmples: vector vi; vector vs; int i; string s; cin >> i >> s; vi.push_back(i); vs.push_back(s); cout << vi[0] << vs[0];

Address of a Variables & Operator & returns the address of an variable e.g.: &i, &ia[0], &ia[1] The address of a memory is expressed as a hex number e.g.: 0xffaabbcc Pointers in C and C++ Pointer A pointer is a named memory space. The size of the memory is determined by the OS (32 bits, 64 bits, ), and it determines the maximum addressing space of the OS. The memory space of a pointer stores the address of another

memory space. We usually say, the pointer points to the memory space. The size of the pointed memory space is determined by the type of the pointer. A pointer is declared as type * name; Example: int * ip //ip (4 bytes of memory for a 32bit OS) 0xffaabbcc int i = 8; ip = &i; i 8

Recently Viewed Presentations

  • Chapter 16

    Chapter 16

    Gene flow between the new subpopulations stops and the two begin to diverge. Eventually, they become incompatible for mating, creating new species. Debate exists as to whether or not allopatric species are different enough to be considered new species.
  • Papyrus: Projet de dépôt institutionnel de l&#x27;UdeM

    Papyrus: Projet de dépôt institutionnel de l'UdeM

    Pour les communautés qui désirent se prévaloir de Papyrus Définir qui peut déposer Déterminer quels types de documents peuvent être déposés Déterminer si on veut mettre en place une procédure de validation Définir quelles métadonnées doivent être incluses dans la...
  • E-Commerce Introduction

    E-Commerce Introduction

    History of The Internet. Started as a US government project in 1969. The purpose was to create a net that can function even if one center is destroyed in a military attack. - "Hub and spokes" can be useless if...
  • Chronic Obstructive Pulmonary Disease

    Chronic Obstructive Pulmonary Disease

    Arterial blood gas (ABG) analysis provides the best clues as to acuteness and severity . pH usually is near normal due to renal compensation in chronic disease. Generally, consider any pH below 7.3 a sign of acute respiratory compromise ?.
  • Creating Games - University of Missouri-St. Louis

    Creating Games - University of Missouri-St. Louis

    The Plan . We will use computer parts to create our characters in the computer games. We will work to design two kinds of games. PacMan Type Games "Level" Games (such as Super Mario Games)
  • Native Americans of New York State

    Native Americans of New York State

    Native Americans of New York State Mrs. Martin Grade 4 Vocabulary Iroquois Algonquians Haudenosaunee wigwam League of Nations hunter-gatherers Sachems nomads Wampum artifacts Clan archaeologist Longhouse natural resource artifacts archeologists A scientist who learns about the past by studying artifacts...
  • The Periodic Table of Elements

    The Periodic Table of Elements

    Elements in the periodic table are arranged in periods (rows) and groups (columns). Atomic number increases as you move across a row or period. Rows of elements are called periods. The period number of an element signifies the highest unexcited...
  • Germ Theory of Disease - Kyrene School District

    Germ Theory of Disease - Kyrene School District

    He also developed agar, a gelatin like substance whish is used to grow microbe cultures in a petri dish . Florence Nightingale1820-1910. She as an English nurse that was the first to recognize that cleanliness is important and recommended as...