Console Programs Console programs are programs that use text to communicate with the use and...

33
Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from a keyboard. The way to compile a console program in C+ + is as follows:

Transcript of Console Programs Console programs are programs that use text to communicate with the use and...

Page 1: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Console Programs

• Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from a keyboard.

• The way to compile a console program in C++ is as follows:

Page 2: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Introduction to C++

• extensions to C continued:– formal support for polymorphic behavior in which a

derived class may override a base class method simply by providing a method of the same name.

– support for function overloading in which there may be different implementations with a single function name.

– an operator overloading mechanism that is analogous to function overloading

– the ability to pass parameters by reference in addition to the standard pass by value.

– yet another input/output library that may be used in addtion to standard and low level I/O

Page 3: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Integrated Development Environment (IDEs):

• Compiling and running console programs using different free IDEs:

IDE Platform Console programs

Code::blocks Windows/Linux/MacOS Compile console programs using Code::blocks

Visual Studio Express Windows Compile console programs using VS Express 2013

Dev-C++ Windows Compile console programs using Dev-C++

Page 4: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Extensions to C

C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP!

The most significant extensions to C are:

• much stronger type checking. Missing casts that produce warnings in C produce errors in C++

• the introduction of true O-O classes• a formal inheritance mechanism in which derived classes can

specialize parent classes• formal support for polymorphic behavior in which a derived class

may override a base class method simply by providing a method of the same name.

• support for function overloading in which there may be different implementations with a single function name.

• an operator overloading mechanism that is analogous to function overloading

• the ability to pass parameters by reference in addition to the standard pass by value.

• yet another input/output library that may be used in addtion to standard and low level I/O

Page 5: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Class GeneralizationThe class is a generalization of the C structure and can contain:• data, as well as function (method) prototypes or full

implementations• accessibility controls (private, protected, public, friend)

– private members of a class are accessible only from within other members of the class (or from their friends).; in general, data will be private.

– protected members are accessible from members of their same class (and from their friends) but also from members of their derived classes.

Page 6: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Class Generalization• accessibility controls (private, protected, public, friend)

– public members are accessible from anywhere where the object is visible; in general, our functions will be public.

– friend functions can access private data in a class; not preferred by O-O purists, who claim that it is a hacker's way to compensate for bad design (which is often true!!)

Page 7: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Simple Example

• The example to the right defines a class (type) called Box.

• b1 and b2 are objects of the Box class.

• The class has five members: three data members of type double(width, length, and height), with private access and two member functions with public access: Box and volume. Note that only the function declarations are given, not their definitions.

class Box{ public: Box(int w, int l, int h); int volume(); void set_values(int w, int l, int h);

private: int width; int length; int height;}; Box b1;Box *b2;

Page 8: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Simple Example

Notice the difference between the class name and the object name. Box is the class name (i.e. the type); whereas, b1 and b2 are objects of type Box. Box and b1 have the same relationship as int and x in the following declaration:

int x;

where int is the type name (the class) and a is the variable name (the object)

class Box{ public: Box(int w, int l, int h); int volume(); void set_values(int w, int l, int h);

private: int width; int length; int height;}; Box b1;Box *b2;

Page 9: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Simple Example

After the declarations of Box and b1, within the body of the program, we can refer to any of the public members of the object b1 as if they were normal functions or normal variables just by putting the object's name followed by a dot (.) and then the name of the member, all very similar to what we did with structures. For example Box b1(2, 5, 7); double v = b1.volume(); b1.set_values(3, 4, 5);

class Bo{ public: Box( int w, int l, int h); double volume(); void set values(int w, int l, int h);

private: int width; int length; double height;}; Box b1;Box *b2;

Page 10: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Simple Example

In the case of b2, we must first dynamically create a Box object by using the new operator:

b2 = new Box(2, 5, 7);

and invoke volume and set_values by

double v = b2->volume; v->set_values(5, 3, 8);

class Box{ public: Box(double w, double l, double h); double volume();

private: double width; double length; double height;}; Box b1;Box *b2;

Page 11: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Simple Example

• The only members of b1 or b2 that we cannot access from the body of our program outside the Box class are length, width, and height, since they have private access and they can only be referenced from within other members of the Box class.

/* CPSC 2100 box.h specification file for the Box class*/

class Box{ public: Box(double w, double l, double h); double volume();

private: double width; double length; double height;}; Box b1;Box *b2;

Page 12: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete Example// class example // box.h (specification file)class Box { public: Box(); Box(int w, int l, int h); ~Box(); int volume(); void set_values(int w, int l, int h); private: int width; int length; int height;};

// test driver main.c

#include <stdio.h>using namespace std;int main() { Box b1(); Box b2(4, 3, 5); Box *b3 = new Box(5, 7, 2); b1.set_values(5, 7, 3); cout << "b1 volume = " << b1.volume() << endl; cout << "b2 volume = " << b2.volume() << endl; cout << "b3 volume = " << b2->volume() << endl;}

// box.cpp (implementation file)Box::Box() { width = 0; length = 0; height = 0;}

Box::Box(int w, int l, int h) { width = w; length = l; height = h;}

int Box::volume() { return (length * width * height);}

void set_values(int w, int l, int h) { width = w; length = l; height = h;}

Page 13: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete Example// test driver main.c

#include <stdio.h>

using namespace std;

int main() {

Box b1();

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

b1.set_values(5, 7, 3);

cout << "b1 volume = " << b1.volume() << endl;

cout << "b2 volume = " << b2.volume() << endl;

cout << "b3 volume = " << b2->volume() << endl;

}

Another thing to notice in the above program are the three instances ro objects: b1, b2, and b3. Each one has its own member variables and member functions.

Output:b1 volume = 105b2 volume = 60b3 volume = 70

Page 14: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete Example// test driver main.c

#include <iostream>

#include "box.h"

using namespace std;

int main() {

Box b1();

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

b1.set_values(5, 7, 3);

cout << "b1 volume = " << b1.volume() << endl;

cout << "b2 volume = " << b2.volume() << endl;

cout << "b3 volume = " << b2->volume() << endl;

}

Also notice that the call b1.volume() does not give the same result as the call b2.volume() or b3->volume(). This is because each object of class Box has its own variables width, length, and height.

That is the basic concept of object-oriented programming: Data and functions are both members of the object.

Output:b1 volume = 105b2 volume = 60b3 volume = 70

Page 15: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete Example// test driver main.c

#include <iostream>

#include "box.h"

using namespace std;

int main() {

Box b1();

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

b1.set_values(5, 7, 3);

cout << "b1 volume = " << b1.volume() << endl;

cout << "b2 volume = " << b2.volume() << endl;

cout << "b3 volume = " << b2->volume() << endl;

}

We no longer use sets of global variables that we pass from one function to another as parameters, but instead we handle objects that have their own data and functions embedded as members.

Notice that we have not had to give any parameters in any of the calls to b1.volume(), b2.volume(), or b3->volume. These member functions directly used the data members of their respective objects b1, b2, and b3.

Output:b1 volume = 105b2 volume = 60b3 volume = 70

Page 16: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Constructors and DestructorsObjects generally need to initialize variables or assign dynamic memory during the creation process in order to become operative and to avoid returning unexpected values during execution.

A class can include a special function, called a constructor, that is automatically called whenever a new object of the class is created. The constructor function must have the same name as the class and cannot have a return type - not even void.

In the class specification below, the Box class includes two constructors: Box() and Box(int w, int l, int h)

// box.h (specification file)class Box { public: Box(); Box(int w, int l, int h); ~Box(); int volume(); void set_values(int w, int l, int h);

private: int width; int length; int height;};

In main(), notice how the arguments are passed to the constructor at the moment the objects of the Box class are created.

int main() {

Box b1();

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

b1.set_values(5, 7, 3);

cout << "b1 volume = " << b1.volume() << endl;

cout << "b2 volume = " << b2.volume() << endl;

cout << "b3 volume = " << b2->volume() << endl;

}

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

Page 17: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Constructors and Destructors// box.h (specification file)class Box { public: Box(); Box(int w, int l, int h); ~Box(); int volume(); void set_values(int w, int l, int h);

private: int width; int length; int height;};

Constructors cannot be called explicitly as if they were regular member functions. They are only executed when a new object of the class is created.

You can also see how neither the constructor prototype declaration (within the class) nor the latter constructor definition includes a return value -- not even void.

int main() {

Box b1();

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

b1.set_values(5, 7, 3);

cout << "b1 volume = " << b1.volume() << endl;

cout << "b2 volume = " << b2.volume() << endl;

cout << "b3 volume = " << b2->volume() << endl;

}

Page 18: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Destructor// box.h (specification file)class Box { public: Box(); Box(int w, int l, int h); ~Box(); int volume(); void set_values(int w, int l, int h);

private: int width; int length; int height;};

Constructors cannot be called explicitly as if they were regular member functions. They are only executed when a new object of the class is created.

You can also see how neither the constructor prototype declaration (within the class) nor the latter constructor definition includes a return value -- not even void.

int main() {

Box b1();

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

b1.set_values(5, 7, 3);

cout << "b1 volume = " << b1.volume() << endl;

cout << "b2 volume = " << b2.volume() << endl;

cout << "b3 volume = " << b2->volume() << endl;

}

Page 19: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Constructors and DestructorsObjects generally need to initialize variables or assign dynamic memory during the creation process in order to become operative and to avoid returning unexpected values during execution.

A class can include a special function, called a constructor, that is automatically called whenever a new object of the class is created. The constructor function must have the same name as the class and cannot have a return type - not even void.

In the class specification below, the Box class includes two constructors: Box() and Box(int w, int l, int h)

// box.h (specification file)class Box { public: Box(); Box(int w, int l, int h); ~Box(); int volume(); void set_values(int w, int l, int h);

private: int width; int length; int height;};

Page 20: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete ExampleBox::Box() { width = 0; length = 0; height = 0;}

Box::Box(int w, int l, int h) { width = w; length = l; height = h;}

int Box::volume() { return (length * width * height);}

void set_values(int w, int l, int h) { width = w; length = l; height = h;}

The cope resolution operator (::) specifies the class to which the member being declared belongs, granting exactly the same scope properties as if this function definition were directly included within the class definition. For example, in the function, set_values(), for the previous code, we were able to use the variables width, length, and height, which are private members of class Box, which means they are only accessible from members of the Box class.

Page 21: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete ExampleBox::Box() { width = 0; length = 0; height = 0;}

Box::Box(int w, int l, int h) { width = w; length = l; height = h;}

int Box::volume() { return (length * width * height);}

void set_values(int w, int l, int h) { width = w; length = l; height = h;}

The members width, length, and height have private access (remember that if nothing else is said, all members have private access. By declaring them private, we deny access to them from anywhere outside the class, except for friend functions, to set values for those members within the object. Perhaps in a simple example as the Box class, it is difficult to see any utility in protecting these three variables, but in greater projects, it may be very important that values cannot be modified in an unexpected way (unexpected from the point of view of the object).

Page 22: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete ExampleBox::Box() { width = 0; length = 0; height = 0;}

Box::Box(int w, int l, int h) { width = w; length = l; height = h;}

int Box::volume() { return (length * width * height);}

void set_values(int w, int l, int h) { width = w; length = l; height = h;}

The members width, length, and height have private access (remember that if nothing else is said, all members have private access. By declaring them private, we deny access to them from anywhere outside the class, except for friend functions, to set values for those members within the object. Perhaps in a simple example as the Box class, it is difficult to see any utility in protecting these three variables, but in greater projects, it may be very important that values cannot be modified in an unexpected way (unexpected from the point of view of the object).

Page 23: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete ExampleBox::Box() { width = 0; length = 0; height = 0;}

Box::Box(int w, int l, int h) { width = w; length = l; height = h;}

int Box::volume() { return (length * width * height);}

void set_values(int w, int l, int h) { width = w; length = l; height = h;}

The destructor fulfills the opposite functionality. It is automatically called when an object is destroyed, either because its scope of existence has finished or because it it is an object dynamically assigned and it is released using the operator delete.

Page 24: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Complete ExampleBox::Box() { width = 0; length = 0; height = 0;}

Box::Box(int w, int l, int h) { width = w; length = l; height = h;}

int Box::volume() { return (length * width * height);}

void set_values(int w, int l, int h) { width = w; length = l; height = h;} The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must not return a value.

The use of destructor is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed or released when we went to release the memory that hee,object remembered.

Page 25: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

C++ sphere_t Class

sphere_t definition in C (a structure):typedef struct sphere_type { int magic; /* Magic number */ point_t center; /* Location of the center */ double radius; /* distance from center to surface */ void *sphDerived; /* Pointer to derived object data */} sphere_t;

sphere_t definition in C++ (a class):

class sphere_t : public sobj_t { public: // Constructor sphere_t(ifstream &infile);

// Methods void load(ifstream &infile); myvector getcenter(); double getradius(); int hits(myvector &base, myvector &dir, hitinfo_t &hit); void dump();

protected: myvector center; double radius;};

Page 26: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Overloading

Like any other function, a constructor can also be overloaded with more than one function that has the same name but different types or number of parameters.

For overloaded functions, the compiler will call the one whose parameters match the arguments used in the function call. In the case of constructors, which are automatically called when an object is created, the one executed is the one that matches the arguments passed when the object is declared

Page 27: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Overloading// box.h (specification file)class Box { public: Box(); Box(int w, int l, int h); ~Box(); int volume(); void set_values(int w, int l, int h);

private: int width; int length; int height;};

In the above code, b1 was declared with no arguments, so it has been initialized with the constructor that has no parameters (this is called a default constructor), which initializes width, length, and height to 0.

b2 was declared with three arguments, so it has been initialized with the constructor that receives three parameters.

int main() {

Box b1;

Box b2(4, 3, 5);

Box *b3 = new Box(5, 7, 2);

b1.set_values(5, 7, 3);

cout << "b1 volume = " << b1.volume() << endl;

cout << "b2 volume = " << b2.volume() << endl;

cout << "b3 volume = " << b2->volume() << endl;

}

Page 28: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Default Constructors

If you do not declare any constructors, the compiler assumes the class to have a default constructor with no parameters; therefore, after declaring a class like the following:

But as soon as you declare your own constructor for a class, the compiler no longer provides an implicit default constructor. So you have to declare all objects of that class according to the constructor prototype you defined for them.

class CPlusExample {

public:

int a, b, c:

void multiply(int n, int m);

}

Page 29: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Default Constructors

But as soon as you declare your own constructor for a class, the compiler no longer provides an implicit default constructor. So you have to declare all objects of that class according to the constructor prototype you defined for them.

Here we have declared a constructor that two parameters of type int. The following object declaration would be correct

But

is not, since there is no default constructor.

class CPlusExample {

public:

int a, b, c:

CPlusExample(int n, int n);

void multiply(int n, int m);

}

CPlusExample ex(2, 8);

CPlusExample ex;;

Page 30: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Default Constructors

If you do not declare any constructors, the compiler assumes the class to have a default constructor with no parameters; therefore, after declaring a class like the following:

But as soon as you declare your own constructor for a class, the compiler no longer provides an implicit default constructor. So you have to declare all objects of that class according to the constructor prototype you defined for them.

In the above code, b1 was declared with no arguments, so it has been initialized with the constructor that has no parameters (this is called a default constructor), which initializes width, length, and height to 0.

b2 was declared with three arguments, so it has been initialized with the constructor that receives three parameters.

class CPlusExample {

public:

int a, b, c:

void multiply(int n, int m);

}

Page 31: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Interactive I/O using cin and cout

With <iostream> you automatically get the following objects:

cout – a predefined object of the ostream class that displays data to standard output ; corresponds to stdout

cin – a predefined object of the istream class that reads data from standard input; corresponds to stdin

cerr – a predefined object of the ostream class that represents the standard error stream; corresponds to stderr.

clog – a predefined object of the ostream class represents the standard logging stream; is associated with stderr.

endl – outputs an end of line character and flushes the buffer.

Page 32: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Namespaces in C++

• In C++, as in other languages, variables, functions, and objects are program entities that must have names.

• C++ uses namespaces to organize the names of program entities.

• Elements within a namespace may be accessed using the scope resolution operator (::). For example, std::cout refers to the cout stream that is a member of the std namespace.

• For frequently-used namespaces, such as std, a special directive can be placed at the top of the file: using namespace std;

• This directive makes every element inside the std namespace available in the current (usually the global) namespace.

Page 33: Console Programs Console programs are programs that use text to communicate with the use and environment – printing text to screen, reading input from.

Interactive I/O using cin and cout

• C++ uses streams to perform input/output operations

• A stream is an object that allows a program to insert characters to it or extract characters from it.

• The standard input/output stream objects are defined in the header file iostream.

• <iostream> must be included for any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output.