Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Programming Language: Fundamentals and Concepts, Study notes of Programming Methodologies

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

2023/2024

Available from 12/11/2024

myraa
myraa 🇮🇳

6 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Page 1 of 12
The Bhopal School of Social Sciences
(Affiliated to Barkatullah University, Bhopal)
E-Content (Study Material)
for
UNIT I
Part-2
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Programming Language: Fundamentals and Concepts and more Study notes Programming Methodologies in PDF only on Docsity!

The Bhopal School of Social Sciences

(Affiliated to Barkatullah University, Bhopal)

E-Content (Study Material)

for

UNIT – I

Part- 2

1. Introduction to C++

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++:

  • Object Oriented
  • Simple to learn and implement
  • Mid-level programming language
  • Structured programming language
  • Rich Library
  • Memory Management
  • Powerful & Fast

2. History 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.

3. Application of C++:

  • To Design OS
  • Engineering Applications
  • Desktop Applications
  • Game Development
    • Development of new languages
    • Embedded System Programming
    • Compiler Design
    • Design Web Browsers

4. Compiling and Linking:

■ 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.

7. Identifiers

■ Names of variables, functions, arrays, classes etc. created by the programmer. ■ Naming rules:

  • Only alphabets, digits and underscores are permitted
  • Cannot start with a digit
  • Uppercase and lowercase letters are distinct
  • A declared keyword cannot be used as a variable name

8. Constants

■ Fixed values that do not change during the execution of a program ■ Example: const int a=5; cin>>a; //Not valid

9. Basic Data Types

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

10. User Defined Data Types

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:

  • Memory that contains a variety of objects over time
  • Only contains one data member at a time
  • Members of a union share space
  • Conserves storage
  • Only the last data member defined can be accessed ■ union declarations
  • Same as struct union Number { int x; float y; }; union Number value; Class class class_name { data member declarations; function declarations; public: data member declarations; function declarations; };

12. Type Compatibility

■ 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

13. Reference Variables

■ 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;

14. Operators in C++

■ 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

15. Scope Resolution Operator

■ C++ is a block structured language

  • Sequential blocks
  • Nested blocks ■ Same variable name can be used to have different meanings in different blocks ■ Scope of a variable extends from the point of its declaration till the end of the block containing the declaration ■ To access global version of a variable from an inner block scope resolution operator (::) can be used ■ Example: #include<iostream.h> #include<conio.h> int m=10; //global m void main() { int m=20,k; { k=m; int m=30; cout<<k<<“\n”; cout<<m<<“\n”; cout<<::m<<“\n”;

cout<<m<<“\n”; cout<<::m<<“\n”; } Output: 20 30 10 20 10

16. Type Cast Operators and Type Conversion

■ 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

17. Member Dereferencing Operators

■ 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

18. Input-Output statements

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.

  • Input Stream: If the direction of flow of bytes is from the device(for example, Keyboard) to the main memory then this process is called input.
  • Output Stream: If the direction of flow of bytes is opposite, i.e. from main memory to device (display screen) then this process is called output.

19. Conditional statements

C++ supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b C++ has the following conditional statements:
  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed The if Statement Use the if statement to specify a block of C++ code to be executed if a condition is true. Syntax: if ( condition ) { // block of code to be executed if the condition is true } Example: if (age>=18) cout<<”Eligible to vote”; The else Statement Use the else statement to specify a block of code to be executed if the condition is false. Syntax: if ( condition ) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false } Example: if (age>=18) cout<<”Eligible to vote”; else cout<<”Not eligible to vote”; The else if Statement Use the else if statement to specify a new condition if the first condition is false. Syntax: if ( condition1 ) { // block of code to be executed if condition1 is true } else if ( condition2 ) { // block of code to be executed if the condition1 is false and condition2 is true

} 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 }

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The break and default keywords are optional Example: int day = 4; switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; } // Outputs "Thursday" (day 4)

20. Loops