1 Introduction
1 Introduction
CS 201
This slide set covers the basics of C++. You should know most
of these basic concepts from your Java classes. To refresh your
knowledge, you may read Chapters 2, 3, 4, and 5 from your
Deitel & Deitel book.
Let’s convert the following simple Java program to C++
import java.util.*;
public class GradeBook {
3
#include <iostream>
using namespace std;
class GradeBook {
public:
void displayMessage( int courseNo ) {
cout << "Welcome to " << courseNo << endl;
}
};
int main() {
int cno;
GradeBook G1; // Do not use the new operator; it is
// an invalid use (different than Java)
cout << "Enter course no: ";
cin >> cno;
G1.displayMessage( cno );
return 0;
} 4
Declarations
● Variables are declared to store values in the computer’s memory
return 0;
}
5
Data types
long double For instance, short, long, int, and
double char are integer data types that differ
float with respect to the number of bytes that
unsigned long int (or unsigned long)
long int (or long)
they use to represent a value
unsigned int (or unsigned)
int Minimum standards:
unsigned short int (or unsigned short) ● short: 2 bytes
short int (or short)
(minimum range -32,768 to 32,767)
unsigned char
char ● long: 4 bytes
bool (minimum range -2,147,483,648 to
2,147,483,647)
Promotion hierarchy for fundamental data types
● int: either short or long
● char: 1 byte
(minimum range -128 to 127)
6
Let’s extend our GradeBook class by adding
● Overloaded functions (those with the same name but different signatures)
7
Constructors
● They are the functions that are called when an object is created
○ The function name should be the same with the class name
○ It cannot return any value (not even void)
9
10
#include <iostream> allProgram.cpp // file continues
#include <string> void displayMessage( string msg ) {
using namespace std; cout << msg << " ";
cout << getCourseNo() << "!" << endl;
class GradeBook { }
void displayMessage() {
public: cout << "Welcome to CS ";
GradeBook( int no ) { cout << getCourseNo() << "!" << endl;
setCourseNo( no ); }
}
GradeBook( ) { private:
courseNo = 0; int courseNo;
}
}; // end of class
void setCourseNo( int no ) {
if ((no >= 100) && (no <= 999)) int main( ){
courseNo = no; GradeBook gb1( 101 ), gb2;
else{
cout << "invalid grade" << endl; gb2.displayMessage();
courseNo = 0; gb2.setCourseNo( 201 );
} gb2.displayMessage("Hello CS");
}
int getCourseNo() { return 0;
return courseNo; }
}
Let’s place a class in a separate file for reusability
● .cpp file is known as a source-code file
11
Separating interface from implementation
● Interface of a class describes what services class’ clients can use without
revealing how these services are implemented
● Header file includes the class’ interface that gives the class definition
○ Prototypes of public member functions for the client
○ Prototypes of public and private member functions and also data members for the compiler
12
#include preprocessor directive
● It instructs the C++ preprocessor to replace directive with a copy of the
contents of the specified file
● Angle brackets are used to include a file in the C++ Standard Library
○ Preprocessor looks for the specified file only in the C++ Standard Library directory
13
14
#ifndef __GRADEBOOK_H GradeBook.h #include <iostream> GradeBook.cpp
#define __GRADEBOOK_H using namespace std;
#include <string> #include "GradeBook.h"
using namespace std; GradeBook::GradeBook( int no ) {
class GradeBook { setCourseNo( no );
public: }
GradeBook( int no ); GradeBook::GradeBook( ) {
GradeBook( ); courseNo = 0;
void setCourseNo( int no ); }
int getCourseNo( ); void GradeBook::setCourseNo( int no ) {
void displayMessage( string msg ); if ((no >= 100) && (no <= 999))
void displayMessage(); courseNo = no;
else {
private: cout << "invalid grade" << endl;
int courseNo; courseNo = 0;
}; // end of class }
#endif }
int GradeBook::getCourseNo() {
return courseNo;
#include "GradeBook.h" program.cpp }
int main() { void GradeBook::displayMessage( string msg ) {
GradeBook gb1( 101 ), gb2; cout << msg << " ";
gb2.displayMessage(); cout << getCourseNo() << "!" << endl;
gb2.setCourseNo( 201 ); }
gb2.displayMessage("Hello CS"); void GradeBook::displayMessage() {
cout << "Welcome to CS ";
return 0;
cout << getCourseNo() << "!" << endl;
}
}
Compilation and linking processes that produce an executable
Compilation and linking
● Source-code file is compiled to create
its object code (class’ object code)
○ Class implementer only needs to provide the
header file and the object code to the client
16
17
Example:
Write a class function that takes grades from the user and returns
their average to the caller
First draft (may have some bugs)
double GradeBook::computeAvg() {
int grade, sum, count;
● When two operands of the division operator are integers, the result is
truncated (integer division)
○ Fractional part of the resulting quotient is lost
● An implicit conversion will occur when the arguments are of different types
○ When the operands of a binary operator or an assignment operator have different data types
○ When the data types of a function parameter and its argument are different
19
Remarks related with this example and more
● Integer values can be used to represent a boolean value
○ For compatibility with C, which does not have a boolean data type
○ Any nonzero value (compilers typically use 1) represents true and 0 represents false
int N, product;
cin >> N;
product = 1;
for ( int i = 1; i <= N; i++ )
product *= i;
cout << "Factorial of " << N << " is " << product << endl;
22
Example:
Write a code fragment that calls a function (for example, factorial
function) until the user wants to stop
char ans;
int N;
do {
cout << "Enter a non-negative integer: ";
cin >> N;
factorial( N );
Loop body
do { always executes
cout << "Do you want to stop (Y or N)? "; at least once
cin >> ans;
} while ( ans != 'Y' && ans != 'y' &&
ans != 'N' && ans != 'n' );
int grade;
char letter;
● continue statement
○ Can be used for while, do...while, and for statements (just for loops)
○ Skips remaining statements in the loop body