210 likes | 334 Views
Introduction to C++. Compiling & makefile Example program Inline functions Reference variables Unary scope resolution operator Casting Overloading Templates. C++. C++ is an enhanced version of C Object-oriented-programming capabilities Other improvements on C features
E N D
Introduction to C++ • Compiling & makefile • Example program • Inline functions • Reference variables • Unary scope resolution operator • Casting • Overloading • Templates
C++ • C++ is an enhanced version of C • Object-oriented-programming capabilities • Other improvements on C features • Is a “superset” of C • Can compile C programs with C++ compiler • Development • Bjarne Stroustrupat Bell Labs in early 1980’s • From C & Simula-67 • Originally called “C with classes” • Later changed to “C++”
Compiling C++ Programs • For C++ files, end with “.cpp” • In UNIX, use “g++” compiler % g++ program.cpp % ./a.out or: % g++ program.cpp –o output % ./output
Makefile in C++ • Save following as “makefile” output: program.o <tab>g++ program.o -o output <blank line> program.o: program.cpp <tab>g++ -c program.cpp <blank line> • Type in “make” to run the makefile % make g++ -c program.cpp g++ program.o -o output % ./output
//A program that adds 2 integers #include <iostream> using std::cout; using std::endl; int main(){ int int1 = 0; cout << "Enter an integer: "; std::cin >> int1; int int2 = 0; cout << "Enter another integer: "; std::cin >> int2; int sum = int1 + int2; cout<<int1<<"+"<<int2<<"="<<sum<<endl; return 0; }
Comments & Header Files //A program that adds 2 integers • Use “//” at the beginning of each line for comments #include <iostream> • Preprocessor directive • Input/output stream header file • List of standard library header files (p. 560) • For programmer-defined header files • #include “myHeaderFile.h”
Output & Input cout << "Enter an integer: "; • Output values to the screen • Standard output stream (cout), or “see-out” • Stream insertion operator (<<), or “put to” std::cin >> int1; • Obtain a value from the keyboard • Standard input stream (cin), or “see-in” • Stream extraction operator (>>), or “get from”
Namespaces • Used to prevent overlapping names • Defines a scope where names of functions & variables are placed • using statement informs compiler that will use the std namespace • std is namespace for standard C++ library using std::cout; . . . cout << "Enter an integer: "; std::cin >> int1;
#include <iostream> int x = 3; namespace Test{ int x = 7; int cout(void); } void main(){ std::cout<<"x="<<x<<std::endl; // x=3 std::cout<<"Test::x="<<Test::x<<std::endl; // Test::x=7 std::cout<<"Test::cout()="<<Test::cout(); // Test::cout()=10 } int Test::cout(void){return 10;}
//Class Exercise 1 #include <iostream> using namespace std; void main(){ char a[]="Hello World"; a[0]+=0x20; a[6]+=0x20; cout << a << endl; for(int i=0; a[i]; i++) cout<<a[i]; cout<<'\n'; } //See exercise1.cpp
Inline Functions • Putting the word inline before a function “advises” the compiler to generate a copy of the function, instead of making a function call • Help reduce function call overhead, • Increases textual size of program • Used for short functions
Inline Functions #include <iostream> using namespace std; inline int product(int a, int b){return a*b;} void main(){ int int1, int2; cout<<"Enter 2 integers: "; cin>>int1>>int2; cout<<"product="<<product(int1,int2)<<endl; }
Reference Variables • Used as an alias for other variables #include <iostream> using std::cout; using std::endl; void main(){ int a = 7; int &b = a;//put “&” in front of variable a++; cout<<"a="<<a<<" b="<<b<<endl; } //a=8 b=8
Reference Variables • Used in functions for call-by-reference #include <iostream> using std::cout; using std::cin; using std::endl; //use “&” for call-by-reference inline void square(int &a){a = a*a;} void main(){ int a = 2; cout<<"a="<<a<<endl; //a=2 square(a); cout<<"a="<<a<<endl; //a=4 }
Returning a Reference • Can be “dangerous” to return reference variables from a function • If return a reference to a variable declared in a function, the variable must be declared static • If not declared static, then will return an undefined variable with unpredictable results (called a dangling reference) • Reference will refer to an automatic variable (an address on the stack) that is discarded (and possibly written over with a new value) when the function ends
Default Arguments • Can have default values for parameters #include <iostream> #define ONE 1 using namespace std; inline int two(void){return 2;} inline int vol(int a = 3, int b = two(), int c = ONE) {return a*b*c;} void main(){ cout<<"vol="<<vol()<<endl; //vol=6 cout<<"vol="<<vol(4)<<endl; //vol=8 cout<<"vol="<<vol(4,5)<<endl; //vol=20 cout<<"vol="<<vol(4,5,6)<<endl;//vol=120 }
Unary Scope Resolution Operator • “::” provides access to a global variable when it has been hidden by a local variable #include <iostream> using standard std; int x = 5; void main(){ int x = 10; cout<<"global x = "<<::x<<endl; cout<<"local x = "<<x<<endl; } //global x = 5 //local x = 10
Casting • Use static_cast<variable-name>(argument) #include <iostream> using std::cout; using std::endl; void main(){ int f=0; float c=0; cout<<"Fahrenheit\t Celsius\t\n"; for(f=100;f>=0;f-=10){ c = (static_cast<float>(5) / 9) * (f - 32.0); cout<<f<<"\t\t"<<c<<endl; } }
Function Overloading • Functions have same name, but different parameters #include <iostream> using standard std; inline int square(int a){return a*a;} inline double square(double a){return a*a;} void main(){ cout<<"square = "<<square(3)<<endl; //square = 9 cout<<"square = "<<square(3.3)<<endl; //square = 10.89 }
Function Templates • Used for functions that have same operations, but different data types #include <iostream> using namespace std; template<class T> T square(T a){return a*a;} void main(){ cout<<"square = "<<square(3)<<endl; //square = 9 cout<<"square = "<<square(3.3)<<endl; //square = 10.89 }
#include <iostream> /*Class Exercise 2*/ using namespace std; int d = 10; inline int A(char b = 'b', char c = 'c') { return b-c;} inline int A(int b, int c){ return b/c;} void main(){ cout<<A()<<endl; cout<<A('d')<<endl; cout<<A('d','D')<<endl; int d = 20; cout<<A(d,::d)<<endl; int e = 'e', f = 'f'; cout<<A(e,f)<<endl; } //See exercise2.cpp