-
About The Course 0
This Course is Only Available On Our App
Dive-in and start learning! Get offline access to all the course contents!
No items in this sectionVerbal 10
-
Lecture2.1
-
Lecture2.2
-
Lecture2.3
-
Lecture2.4
-
Lecture2.5
-
Lecture2.6
-
Lecture2.7
-
Lecture2.8
-
Lecture2.9
-
Lecture2.10
Critical Reasoning 14
-
Lecture3.1
-
Lecture3.2
-
Lecture3.3
-
Lecture3.4
-
Lecture3.5
-
Lecture3.6
-
Lecture3.7
-
Lecture3.8
-
Lecture3.9
-
Lecture3.10
-
Lecture3.11
-
Lecture3.12
-
Lecture3.13
-
Lecture3.14
Abstract Reasoning 8
-
Lecture4.1
-
Lecture4.2
-
Lecture4.3
-
Lecture4.4
-
Lecture4.5
-
Lecture4.6
-
Lecture4.7
-
Lecture4.8
Technical Assesment 6
-
Lecture5.1
-
Lecture5.2
-
Lecture5.3
-
Lecture5.4
-
Lecture5.5
-
Lecture5.6
Coding 4
-
Lecture6.1
-
Lecture6.2
-
Lecture6.3
-
Lecture6.4
Previous year Question Paper With Solution 7
-
Lecture7.1
-
Lecture7.2
-
Lecture7.3
-
Lecture7.4
-
Lecture7.5
-
Lecture7.6
-
Lecture7.7
Model Question Paper With Explanation 3
-
Lecture8.1
-
Lecture8.2
-
Lecture8.3
CPP Programming1. Introduction To C++• Limitations Of C Language• A Brief History of C++ And Characteristics• C++ Organization• Object Oriented Programming• Basic I/O in C++• Data Types In C++• Variable• Escape sequences• Constants And Keywords2.Object Oriented Programming System in C++• OOPS in C++• Classes in C++• Objects in C++• Abstraction In C++• Encapsulation In C++• Inheritance in C++• Polymorphism in C++3.Functions, Constructors, Structures andKeywords in C++• Function definition• Constructors In C++• Structures in C++• const Keyword in C++• static Keyword in C++4. [Bonus] Extra Topic• Exception Handling in C++• Initializer List in C++• Header Guards in C++1. Introduction To C++★ Limitations Of C LanguageThe limitations of C programming languages are as follows:• Difficult to debug.• C allows a lot of freedom in writing code, and that is why you can put anempty line or white space anywhere in the program. And because there is nofixed place to start or end the line, so it isn't easy to read and understand theprogram.• C compilers can only identify errors and are incapable of handling exceptions(run-time errors).• C provides no data protection.• It also doesn't feature the reusability of source code extensively.• It does not provide strict data type checking (for example, an integer value canbe passed for floating datatype).Categorisation Of High Level LanguagesHigh-level programming languagesThe high-level programming languages can be categorized into different types onthe basis of the application area in which they are employed as well as thedifferent design paradigms supported by them. The high-level programminglanguages are designed for use in a number of areas. Each high-level language isdesigned by keeping its target application area in mind. Some of the high-levellanguages are best suited for business domains, while others are apt in the scientificdomain only. The high-level language can be categorized on the basis of the variousprogramming paradigms approved by them. The programming paradigms refer to theapproach employed by the programming language for solving the different types ofproblem.1. Categorisation based on ApplicationOn the basis of application area the high level language can be divided into thefollowing types:i) Commercial languagesThese programming languages are dedicated to the commercial domain and arespecially designed for solving business-related problems. These languages can beused in organizations for processing handling the data related to payroll, accountspayable and tax building applications. COBOL is the best example of the commercialbased high-level programming language employed in the business domain.ii) Scientific languagesThese programming languages are dedicated to the scientific domain and arespecially designed for solving different scientific and mathematical problems. Theselanguages can be used to develop programs for performing complex calculationduring scientific research. FORTRAN is the best example of scientific basedlanguage.iii) Special purpose languagesThese programming languages are specially designed for performing some dedicatedfunctions. For example, SQL is a high-level language specially designed to interactwith the database programs only. Therefore we can say that the special purposehigh-level language is designed to support a particular domain area only.iv) General purpose languagesThese programming languages are used for developing different types of softwareapplications regardless of their application area. The various examples of generalpurpose high-level programming languages are BASIC, C, C++, and java.2.Categorisation based on Design paradigmOn the basis of design paradigms the high level programming languages can becategorised into the following types:i) Procedure-oriented languagesThese programming languages are also called an imperative programming language.In this language, a program is written as a sequence of procedures. Each procedurecontains a series of instructions for performing a specific task. Each procedure can becalled by the other procedures during the program execution. In this type ofprogramming paradigms, a code once written in the form of a procedure can be usedany number of times in the program by only specifying the corresponding procedurename. Therefore the procedure-oriented language allows the data to move freelyaround the system. The various examples of procedure-oriented language areFORTRAN, ALGOL, C, BASIC, and ADA.ii) Logic-oriented languagesThese languages use logic programming paradigms as the design approach forsolving various computational problems. In this programming paradigm predicatelogic is used to describe the nature of a problem by defining the relationship betweenrules and facts. Prolog is the best example of the logic-oriented programminglanguage.iii) Object-oriented languagesThese languages use object-oriented programming paradigms as the design approachfor solving a given problem. In this programming language, a problem is divided intoa number of objects which can interact by passing messages to each other. C++ and C# areexamples of object-oriented programming languages.★ A Brief History of C++• The history of C++ begins with C. The reason for this is easy to understand:C++ is built upon the foundation of C. Thus, C++ is a superset of C.• C++ expanded and enhanced the C language to support object-orientedprogramming (which is described later in this module).• C++ also added several other improvements to the C language, including anextended set of library routines. However, much of the spirit and flavor of C++is directly inherited from C. Therefore, to fully understand and appreciate C++,you need to understand the “how and why” behind C.• C++ is regarded as a middle-level language, as it comprises a combination ofboth high-level and low-level language features.• The C++ programming language was created by Bjarne Stroustrup and his teamat Bell Laboratories (AT&T, USA) to help implement simulation projects in anobject-oriented and efficient way. The earliest versions, which were originallyreferred to as “C with classes,” date back to 1980. As the name C++ implies,C++ was derived from the C programming language: ++ is the incrementoperator in C.Characteristics of C++C++ is not a purely object-oriented language but a hybrid that contains thefunctionality of the C programming language. This means that you have all thefeatures that are available in C:• Universally usable modular programs• Efficient, close to the machine programming• Portable programs for various platforms.★ C++ Organization• C++ is designed as a bridge between the programmer and the raw computer.• The idea is to let the programmer organize a program in a way that he or shecan easily understand.• The compiler then translates the language into something the machine can use.• Computer programs consist of two main parts: data and instructions. Thecomputer imposes little or no organization on these two parts.• After all, computers are designed to be as general as possible. The idea is forthe programmer to impose his or her own organization on the computer andnot the other way around.C++ Compilation ProcessIt is fundamental to know how C++ compilation works to understand how programsare compiled and executed. Compiling C++ source code into machine-readable codeconsists of the following four processes:1. Preprocessing the source code.2. Compiling the source code.3. Assembling the compiled file.4. Linking the object code file to create an executable file.Let's start with a simple C++ program to understand how compilation happens.#include <iostream>int main(){// This is a single line comment/* This is a multi-linecomment */std::cout << "Hello Universe" << std::endl;return 0;}Let's demystify the C++ compilation process using the following diagram:1. When the C++ preprocessor encounters the #include <file> directive, itreplaces it with the content of the file creating an expanded source code file.2. Then, this expanded source code file is compiled into an assembly languagefor the platform.3. The assembler converts the file that's generated by the compiler into the objectcode file.4. This object code file is linked together with the object code files for anylibrary functions to produce an executable file.Difference Between Header and Source FilesSource files contain the actual implementation code. Source files typically have theextension .cpp, although other extensions such as .cc, .ccx, or .c++ are also quitecommon.On the other hand, header files contain code that describes the functionalities that areavailable. These functionalities can be referred to and used by the executable code inthe source files, allowing source files to know what functionality is defined in othersource files.Compiling And Linking ProcessCompilation is a process that ensures that a program is syntactically correct, but itdoes not perform any checks regarding its logical correctness. This means that aprogram that compiles correctly might still produce undesired results:Every C++ program needs to define a starting point, that is, the part of the code theexecution should start from. The convention is to have a uniquely named mainfunction in the source code, which will be the first thing to be executed. This functionis called by the operating system, so it needs to return a value that indicates the status ofthe program; for this reason, it is also referred to as the exit status code.★ Object-Oriented ProgrammingCentral to C++ is object-oriented programming (OOP). As just explained, OOP wasthe impetus for the creation of C++. Because of this, it is useful to understand OOP’sbasic principles before you write even a simple C++ program.Object-oriented programming offers several major advantages to softwaredevelopment:• Reduced susceptibility to errors: an object controls access to its own data.More specifically, an object can reject erroneous access attempts.• Easy re-use: objects maintain themselves and can therefore be used asbuilding blocks for other programs.• Low maintenance requirement: an object type can modify its own internaldata representation without requiring changes to the application.A First Simple C++ Program/* This is a simple C++ program. Call this file Sample.cpp. */#include<iostream>using namespace std;// A C++ program begins at main().int main(){cout << "C++ is power programming.";return 0;}Output/* When run, the program displays the following output:*/C++ is power programming.★ Basic I/O in C++The C++ standard libraries provide an extensive set of input/output capabilities whichwe will see in subsequent chapters. This chapter will discuss very basic and mostcommon I/O operations required for C++ programming.C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a devicelike a keyboard, a disk drive, or a network connection etc. to main memory, this iscalled input operation and if bytes flow from main memory to a device like a displayscreen, a printer, a disk drive, or a network connection, etc., this is called outputoperation.I/O Library Header FilesThere are following header files important to C++ programs −The Standard Output Stream (cout)The predefined object cout is an instance of ostream class. The cout object is said tobe "connected to" the standard output device, which usually is the display screen. Thecout is used in conjunction with the stream insertion operator, which is written as <<which are two less than signs as shown in the following example.#include <iostream>using namespace std;int main() {char str[] = "Hello C++";cout << "Value of str is : " << str << endl;}When the above code is compiled and executed, it produces the following result −Value of str is : Hello C++The C++ compiler also determines the data type of variable to be output and selectsthe appropriate stream insertion operator to display the value. The << operator isoverloaded to output data items of built-in types integer, float, double, strings andpointer values.The insertion operator << may be used more than once in a single statement as shownabove and endl is used to add a new-line at the end of the line.The Standard Input Stream (cin)The predefined object cin is an instance of istream class. The cin object is said to beattached to the standard input device, which usually is the keyboard. The cin is usedin conjunction with the stream extraction operator, which is written as >> which aretwo greater than signs as shown in the following example.#include <iostream>using namespace std;int main() {char name[50];cout << "Please enter your name: ";cin >> name;cout << "Your name is: " << name << endl;}When the above code is compiled and executed, it will prompt you to enter a name.You enter a value and then hit enter to see the following result −Please enter your name: cplusplusYour name is: cplusplusThe C++ compiler also determines the data type of the entered value and selects theappropriate stream extraction operator to extract the value and store it in the givenvariables.The stream extraction operator >> may be used more than once in a single statement.To request more than one datum you can use the following −cin >> name >> age;This will be equivalent to the following two statements −cin >> name;cin >> age;The Standard Error Stream (cerr)The predefined object cerr is an instance of ostream class. The cerr object is said tobe attached to the standard error device, which is also a display screen but the objectcerr is un-buffered and each stream insertion to cerr causes its output to appearimmediately.The cerr is also used in conjunction with the stream insertion operator as shown inthe following example.#include <iostream>using namespace std;int main() {char str[] = "Unable to read";cerr << "Error message : " << str << endl;}When the above code is compiled and executed, it produces the following result −Error message : Unable to readThe Standard Log Stream (clog)The predefined object clog is an instance of ostream class. The clog object is said tobe attached to the standard error device, which is also a display screen but the objectclog is buffered. This means that each insertion to clog could cause its output to beheld in a buffer until the buffer is filled or until the buffer is flushed.The clog is also used in conjunction with the stream insertion operator as shown inthe following example.#include <iostream>using namespace std;int main() {char str[] = "Unable to read";clog << "Error message : " << str << endl;}When the above code is compiled and executed, it produces the following result −Error message : Unable to readYou would not be able to see any difference in cout, cerr and clog with these smallexamples, but while writing and executing big programs the difference becomesobvious. So it is good practice to display error messages using cerr stream and whiledisplaying other log messages then clog should be used.★ Datatypes In C++There are 4 types of data types in C++ language.Basic Data TypesThe basic data types are integer-based and floating-point based. C++ languagesupports both signed and unsigned literals.The memory size of basic data types may change according to the 32 or 64 bitoperating system.Let's see the basic data types. Its size is given according to 32 bit OS.Derived Data TypesThe data-types that are derived from the primitive or built-in datatypes are referred toas Derived Data Types. These can be of four types namely:1. Function2. Array3. Pointer4. ReferenceAbstract or User-Defined Data TypesThese data types are defined by user itself. Like, defining a class in C++ or astructure. C++ provides the following user-defined datatypes:1. Class2. Structure3. Union4. Enumeration5. Typedef defined DataTypeDatatype ModifiersAs the name implies, datatype modifiers are used with the built-in data types tomodify the length of data that a particular data type can hold.Data type modifiers available in C++ are:Signed• Unsigned• Short• LongBelow table summarizes the modified size and range of built-in datatypes whencombined with the type modifiers:★ VariablesA variable is a name of memory location. It is used to store data. Its value can bechanged and it can be reused many times.It is a way to represent memory location through symbol so that it can be easilyidentified.Let's see the syntax to declare a variable:1. int x;2. float y;3. char z;Here, x, y, z are variables and int, float, char are data types.We can also provide values while declaring the variables as given below:1. int x=5,b=10; //declaring 2 variable of integer type2. float f=30.8;3. char c='A';Rules for defining variables1. A variable can have alphabets, digits and underscore.2. A variable name can start with an alphabet and underscore only. It can't startwith a digit.3. No white space is allowed within the variable name.4. A variable name must not be any reserved word or keyword e.g. char, floatetc.5. Valid variable names:• int a;• int _ab;• int a30;6. Invalid variable names:• int 4;• int x y;• int double;Scope Of VariablesIn general, the scope is defined as the extent up to which something can be workedwith. In programming also the scope of a variable is defined as the extent of theprogram code within which the variable can be accessed or declared or worked with.There are mainly two types of variable scopes:1. Local Variables2. Global VariablesNow let’s understand each of the scope at a greater detail:Local VariablesVariables defined within a function or block are said to be local to those functions.• Anything between ‘{‘ and ‘}’ is said to inside a block.• Local variables do not exist outside the block in which they are declared, i.e.they can not be accessed or used outside that block.• Declaring local variables: Local variables are declared inside a block.// CPP program to illustrate// usage of local variables#include<iostream>using namespace std;void func(){// this variable is local to the// function func() and cannot be// accessed outside this functionint age=18;}int main() {cout<<"Age is: "<<age;return 0;}Output:Error: age was not declared in this scopeThe above program displays an error saying “age was not declared in this scope”. Thevariable age was declared within the function func() so it is local to that function andnot visible to portion of program outside this function.Rectified Program : To correct the above error we have to display the value ofvariable age from the function func() only. This is shown in the below program:// CPP program to illustrate// usage of local variables#include<iostream>using namespace std;void func(){// this variable is local to the// function func() and cannot be// accessed outside this functionint age=18;cout<<age;}int main(){cout<<"Age is: ";func();return 0;}Output:Age is: 18Global VariablesAs the name suggests, Global Variables can be accessed from any part of theprogram.• They are available throughout the lifetime of a program.• They are declared at the top of the program outside all of the functions orblocks.• Declaring global variables: Global variables are usually declared outside ofall of the functions and blocks, at the top of the program. They can beaccessed from any portion of the program.// CPP program to illustrate// usage of global variables#include<iostream>using namespace std;// global variableint global = 5;// global variable accessed from// within a functionvoid display(){cout<<global<<endl;}// main functionint main(){display();// changing value of global// variable from main functionglobal = 10;display();}Output:510In the program, the variable “global” is declared at the top of the program outside allof the functions so it is a global variable and can be accessed or updated fromanywhere in the program.What if there exists a local variable with the same name as that of globalvariable inside a function?Let us repeat the question once again. The question is : if there is a variable inside afunction with the same name as that of a global variable and if the function tries toaccess the variable with that name, then which variable will be given precedence?Local variable or Global variable? Look at the below program to understand thequestion:// CPP program to illustrate// scope of local variables// and global variables together#include<iostream>using namespace std;// global variableint global = 5;// main functionint main(){// local variable with same// name as that of global variableint global = 2;cout << global << endl;}Look at the above program. The variable “global” declared at the top is global andstores the value 5 whereas that declared within the main function is local and stores avalue 2. So, the question is when the value stored in the variable named “global” isprinted from the main function then what will be the output? 2 or 5?• Usually when two variable with the same name are defined then the compilerproduces a compile time error. But if the variables are defined in differentscopes then the compiler allows it.• Whenever there is a local variable defined with same name as that of a globalvariable then the compiler will give precedence to the local variable★ Escape SequencesEscape sequences are used in the programming languages C and C++ , and theirdesign was copied in many other languages such as Java and C# . Escape sequencesare the special characters used in control string to modify the format of the output.These characters are not displayed in output. These characters are used withcombination of backslash \. This backslash \ is called escape character.Table of Escape sequence of C & C++ are as follows:★ ConstantsConstants refer to fixed values that the program may not alter and they are calledliterals.Constants can be of any of the basic data types and can be divided into IntegerNumerals, Floating-Point Numerals, Characters, Strings and Boolean Values.Again, constants are treated just like regular variables except that their values cannotbe modified after their definition.KeywordsThis is a list of reserved keywords in C++. Since they are used by the language, thesekeywords are not available for re-definition or overloading.C++ Sample
Prev C++Next Java Programming