Mastering C++: A Comprehensive Guide to Object-Oriented Programming Features
C++ has become the standard for object-oriented programming, offering robust features such as data encapsulation, inheritance, and polymorphism. This guide covers foundational concepts, essential tools, and new features introduced in C++11, including lambda expressions and move semantics. Learn how to implement classes, manipulate data types, and utilize control structures to create efficient C++ programs. With practical examples and a review of modern C++ capabilities, this resource is designed for beginners and experienced programmers alike who seek to enhance their skills in this powerful language.
Mastering C++: A Comprehensive Guide to Object-Oriented Programming Features
E N D
Presentation Transcript
C++ • A word concerning C++ and Object Oriented (OO) Programming • C++ became the de facto standard and grew from the need of an object oriented programming method. • OO programming can be accomplished by virtually any language. • C, assembly, some proprietary languages can be used. • C++ formally standardized OO based upon the C standard. • C++ is a tool that allows data encapsulation to be combined with operations (methods) into a data structure known as a class. • C++ allows its data structures to be used as building blocks for other data structures known as inheritance. • C++ allows a concept known as polymorphism which is the ability of a method or operator to be used in varying ways. The method of the derived class redefines the method of the base class (virtual).
C++ • Review • What do we need to have a C++ program? • Have to have an idea of what you want to do. • Have to have a main() • Have to have some objects that you want to use. • <iostream> • std namespace • Have to have tools to compile, edit, and run the program.
C++ • Review cont’d • Declarations (Data Set) • Types • Integer (int) ; real numbers (float); Boolean (bool) • Operations (Data Manipulation) • Types • Addition (+); Assignment (=); Comparators (< > ==) • Statements (Determines Program Control) • Types • Loops (for, while); Decisions (if, switch); Declarative (continue, return) • Preprocessor Directives • Types • Definitions (#define); Include (#include)
C++ • Review of new C++11 Features • Templates no longer have to be closed with space b/t close template expressions • Null point type • Automatic Type Deduction • Uniform initialization • For Loop Changes • Move Semantics • String Literals • No exception • Constant Expressions • New Template Features • Lambdas • Declaration Types • New Function Declarations • Scoped Enumerations
C++ For loop changes Range-based for loops similar to shell programming for (inti : {1,2,4,16,256}) { do something } OR #define OK 0 Uniform initmap<string,string> myMap { {“ip”,”10.10.10.10”},{“brd”,”10.10.10.255”},{“ip”,”192.168.0.1”},{“brd”,””}} for (auto& i : myMap) Automatic Type deduction { if(i[“ip”].compare(“192.168.0.1”) == OK) { do something here …. } }
C++ Move Semantics What’s it good for? Efficiency of not having to make an expensive copy. The move will merely copies the reference of the thing in memory to a target location. #include <iostream> #include <utility> #include <vector> #include <string> std::string str = "Hello"; std::vector<std::string> v; v.push_back(str); Place “Hello” in the first place of the vector, it is copied. v.push_back(std::move(str)); move the reference into the second place in the vector. What happened to str?
C++ New Template Features • Variadic Templates • Aliased Templates • Default Template and local types arguments Variadic template <size_t… things> struct ArrayOfThings { size_t theArray[] = { things… }; } Aliased Template #include<iostream> #include<bitset> template <typename T> using strAlias= std::map< std::string, T >; EnummyBits {zero,one,two,three}; strAlias<bitset<4>> bitString; bitString[“first”].set(zero) ; bitString[“BitArray”].set(zero); bitString[“second”].set(one); bitString[“BitArray”].set(one); Default Argument Template <typename T, typename container = vector<T>>; class DefVector DefVector<int> Vec;
C++ Lambdas Inline Expressions A method in which an anonymous function can be defined. These are of the form [capture](parameters)->(type){body}. A bit more powerful than creating MACRO s in C. [ ](int x, int y) { return x + y; } Implicit return [ ](int x, int y) ->(int){ return x + y; } Explicit return [ ]{ ++x; } No parameters Closures A lambda function can refer to identifiers/parameters declared outside the lambda function. Closures are defined between the brackets [ ] of lambda expressions. [] //no variables defined. Attempting to use any external variables in the lambda is an error. [x, &y] //x is captured by value, y is captured by reference [&] //any external variable is implicitly captured by reference if used [=] //any external variable is implicitly captured by value if used [&, x] //x is explicitly captured by value. Other variables will be captured by reference [=, &z] //z is explicitly captured by reference. Other variables will be captured by value
C++ • Utilities • Smart Pointers • Pairs and Tuples • Type Traits and Type Utilities • Clocks and Timers • Miscellaneous C Functions
C++ • Lab Notes Cygwin on the PC provides • GNU compiler • Editors like vi and emacs • Gnu debugger • Terminal emulation such as xterm for vt100. • Python and bash shell languages and scripting. • SVN client access GNU Compiler (4.5.3) • g++ -v returns the g++ version • g++ -std=gnu++0x applies support for C++11 SVN (http://www.computerarcheology.com/cpp) • svn co http://www.computerarcheology.com/cpp • cpp advanced docs sources