You are on page 1of 3

iostream.

h
• iostream.h replaces stdio.h
• cin replaces “scanf”
From C to C++ • cout replaces “printf”

Daniel Wigdor
Some material from:
Winston, P.H., “On to C++”
ISBN: 0-201-58043-8

Using CIN & COUT New Comments


• Using cout and the << operator: • Multi-line comments same as C (/* */)
• C++ allows single-line comments (//).
cout << “Hello ” << “world” << endl;
• Eg:
/* this is a
• Using cin and the >> operator: multi-line comment */
int x,y ; cout << “hello”;
cin >> x >> y; // this is a single -line comment
cout << “ world” << endl;

main() function Classes


• C++ programs are executed by running • C++ adds the idea of a class
the “main” function. • Kind of like a struct, but different
• Return type can now be omitted. • Group of data (like a struct), also a group
• Eg: of functions (not like a struct).
#include <iostream.h >
main() {
• Adds concept of “public” and “private” data
cout << “Hello world”<<endl;
}
Class Definition Instantiating & Using a Class
#include <iostream.h >
• Variables within a class are called class BoxCar {
“members”. };
public: double height, width, length;

• Basic class definition of a railway box car: double volume (BoxCar b) {


return b.height * b.width * b.length;
}
class BoxCar { main() {
public: BoxCar x;
double height, width, length; x.height = 10.5; x.width = 9.5; x.length = 40.0;
}; cout << “The volume of the box car is “ <<
volume(x) << endl;
}

Adding Member Functions Using Member Functions


• Functions that perform operations on a class BoxCar {
public: double height, width, length;
class may be added to that class. double volume() {
return height * width * length;
• Adding the “volume” function to BoxCar: }
class BoxCar { };
public: double height, width, length; main() {
double volume() { BoxCar x;
return height * width * length; x.height = 10.5; x.width = 9.5; x.length = 40.0;
} cout << “The volume of the box car is “ <<
}; x.volume() << endl;
}

Separating Prototypes & Definitions Adding a “Constructor”


• Member functions can be defined • A constructor is run every time a class is
separately from their prototype. Eg: “instantiated”.
class BoxCar {
public: double height, width, length;
• Allows you to set “default” values for
double volume(); member variables.
};
double BoxCar::volume() {
return height * width * length;
}
Constructor Example Constructors with Parameters
class BoxCar {
public: • Allows client to set initial values.
double height, width, length;
// default constructor class BoxCar {
public:
BoxCar() {height=10.0, width=10.0, length=10.0;}
double height, width, length;
double volume() { return height*width*length;} // default constructor
}; BoxCar() {height=10.0, width=10.0, length=10.0;}
main() { // constructor with parameters
BoxCar x; BoxCar(double h, double w, double l) {
height = h; width = w; length = l;
cout << “The volume of the box car is “ <<
}
x.volume() << endl; double volume() { return height*width*length;}
} };

Using Constructor
class BoxCar {
public:
double height, width, length;
// default constructor
BoxCar() {height=10.0, width=10.0, length=10.0;}
// constructor with parameters
BoxCar(double h, double w, double l) {
height = h; width = w; length = l;
}
double volume() { return height*width*length;}
};
main() {
BoxCar x (5.0,5.0,5.0);
cout << “The volume of the box car is “ <<
x.volume() << endl;
}

You might also like