






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
A comprehensive introduction to the c++ programming language, covering its history, features, basic data types, user-defined data types, operators, type compatibility, and fundamental programming concepts like conditional statements and loops. It serves as a valuable resource for beginners seeking to learn the core principles of c++ programming.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!
C++ was developed by Bjarne Stroustrup (Bell Labs, 1979) and it started as extension to C (macros and variables). C++ added new useful features to C. Features of C++:
Year Language Developed by Remarks 1960 ALGOL International Committee Too general, Too abstract 1963 CPL (Combined PL) Cambridge University Hard to learn, Difficult to implement 1967 BCPL (Basic CPL) Martin Richards Could deal with only specific problems 1970 B Ken Thompson AT & T Bell Labs Could deal with only specific problems 1972 C Dennis Ritchie AT & T Bell Labs Lost generality of BCPL and B restored Early 80’s C++ Bjarne Stroustrup AT & T Introduces OOPs.
■ After writing a C++ program, the next step is to compile the program before running it. ■ The compilation is the process which converts the program written in human readable language like C, C++ etc. into a machine code, directly understood by the Central Processing Unit. ■ There are many stages involved in creating a executable file from the source file. The stages include Preprocessing, Compiling and Linking in C++. ■ This means that even if the program gets compiled, it may result in not running as errors may arise during the linking phase.
■ Names of variables, functions, arrays, classes etc. created by the programmer. ■ Naming rules:
■ Fixed values that do not change during the execution of a program ■ Example: const int a=5; cin>>a; //Not valid
Type Bytes Range Char 1 - 128 to 127 unsigned char 1 0 to 255 signed char 1 - 128 to 127 Int 2 - 32768 to 32767 unsigned int 2 0 to 65535 signed int 2 - 32768 to 32767 short int 2 - 32768 to 32767 unsigned short int 2 0 to 65535 signed short int 2 - 32768 to 32767 long int 4 - 2147483648 to 2147483647 signed long int 4 - 2147483648 to 2147483647 unsigned long int 4 0 to 4294967295 Float 4 3.4E-38 to 3.4E+ Double 8 1.7E-308 to 1.7E+ long double 10 3.4E-4932 to 1.1E+ C++ Data Types User Defined Data Type Structure Union Class Enumeration Built-in Type Integral Type int char Void Floating Type float double Derived Type Array Function Pointer Reference
Structure ■ C++ structures allow variables to be grouped to form a new data type. ■ The data elements in a structure are arranged in a manner that is similar to the way database programs arrange data. ■ C++ allows you to create a record by grouping the variables and arrays necessary for the fields into a single structure. ■ Example: struct inventory_item { int quantity_on_hand; int reorder_point; double cost; double retail_price; }; ■ Once you have declared a structure, you must declare an identifier, called a structure variable, that is of the structure’s type. ■ The statement below creates a structure variable of a structure named todays_special that is of type inventory_item. inventory_item todays_special; ■ To access a member of the structure, use the name of the variable, the dot operator, then the name of the member you need to access. todays_special.cost=47.80; Unions ■ Union is a data type such that:
■ C++ is very strict with regard to type compatibility ■ C++ defines int, short int and long int as three different types. They must be cast when their values are assigned to one another ■ Necessary for function overloading
■ Provides an alias (alternative name) for a previously defined variable ■ Must be initialized at the time of declaration ■ Syntax: data-type & reference-name=variable-name; ■ Example: float total=100; float &sum=total;
■ All C operators are valid in C++ ■ Operator overloading is possible in C++ Operator Operator Name << Insertion operator
Extraction operator :: Scope resolution operator ::* Pointer-to-member declarator
- Pointer-to-member operator .* Pointer-to-member operator delete Memory release operator endl Line feed operator new Memory allocation operator setw Field width operator
■ C++ is a block structured language
cout<<m<<“\n”; cout<<::m<<“\n”; } Output: 20 30 10 20 10
■ C++ permits explicit type conversion of variables or expressions using the type cast operator ■ Syntax: (type-name) expression //C notation type-name (expression) //C++ notation ■ Example: average=sum/(float) i; //C notation average=sum/float (i); //C++ notation
■ C++ permits users to make classes with data members and member functions ■ Class members can be accessed through pointers Operator Operator Name ::* To declare a pointer to a member of a class
- To access a member using a pointer to the object and a pointer to that member .* To access a member using object name and a pointer to that member class X { public: int a; void f(int b) { cout << "The value of b is "<< b << endl; } }; void main() { // declare pointer to data member int X::iptr = &X::a; // declare a pointer to member function void (X:: fptr) (int) = &X::f; // create an object of class type X X xobject; // initialize data member xobject.iptr = 10; cout << "The value of a is " << xobject.iptr << endl; // call member function (xobject.*fptr) (20); } OUTPUT: The value of a is 10 The value of b is 20
C++ comes with libraries that provide us with many ways for performing input and output. In C++ input and output are performed in the form of a sequence of bytes or more commonly known as streams.
C++ supports the usual logical conditions from mathematics:
} else { // block of code to be executed if the condition1 is false and condition2 is false } Example: int time = 22; if (time < 10) { cout << "Good morning."; } else if (time < 20) { cout << "Good day."; } else { cout << "Good evening."; } // Outputs "Good evening." Ternary Operator There is also a short-hand if else, which is known as the ternary operator because it consists of three operands. It can be used to replace multiple lines of code with a single line. It is often used to replace simple if else statements: Syntax: variable = ( condition )? expressionTrue : expressionFalse ; Example: int time = 20; string result = (time < 18)? "Good Day." : "Good Evening."; cout<<result; Switch Statement Use the switch statement to select one of many code blocks to be executed. Syntax: switch(expression) { case x: // code block break; case y: // code block break; default: // code block }