C++ Programming PDF
Document Details
Silver Oak University
Tags
Summary
These lecture notes cover the fundamentals of C++ programming, including Object-Oriented Programming concepts. The document details procedural and object-oriented programming paradigms, and introduces classes, objects, and data types.
Full Transcript
Chapter 1: OOPC Introduction:- Topics: - Procedural Vs. Object Oriented Programming Basic concepts of OOP Benefits and applications of OOP Program structure Identifiers variables constants Tokens Expressions Control Structures Simple I/O statements- reading and...
Chapter 1: OOPC Introduction:- Topics: - Procedural Vs. Object Oriented Programming Basic concepts of OOP Benefits and applications of OOP Program structure Identifiers variables constants Tokens Expressions Control Structures Simple I/O statements- reading and writing Data types Enum Operators Typecasting Control structures Reference variable Control statements Looping statements Types of Errors Array ❖ Procedural Vs. Object Oriented Programming Procedure Oriented Programming (POP) Object Oriented Programming (OOP) 1. Emphasis is on doing things not on data, 1. Emphasis is on data rather than means it is function driven procedure, means object driven 2. Main focus is on the function and procedures 2. Main focus is on the data that is being operated that operate on data 3. Top Down approach in program design 3. Bottom Up approach in program design 4. Large programs are divided into rams known as 4. Large programs are divided into classes and objects functions 5. Most of the functions share global data 5. Data is structured together with function in the data. 6. Data moves openly in the system from one 6. Data is hidden and cannot be accessed by function to another function external functions 7. Adding of data and function is difficult 7. Adding of data and function is easy 8. We cannot declare namespace directly 8. We can use namespace directly, Ex: using namespace std; 9. Concepts like inheritance, polymorphism, data 9. Concepts like inheritance, polymorphism, data encapsulation, abstraction, access specifiers are not encapsulation, abstraction, access specifiers are available available. and can be used easily 10. Examples: C, Fortran, Pascal, etc… 10. Examples: C++, Java, C#, etc… ❖ Introduction to Object Oriented Programming : C++ can be considered as an incremental version of c language which consist all programming language constructs with newly added features of object oriented programming. c++ is structure(procedure) oriented and object oriented programming language. The file extension of C++ program is “.CPP” Function overloading and operator overloading are possible. Variables can be declared in inline i.e when required In c++ more emphasis is give on data rather than procedures Polymorphism, encapsulation and inheritance are possible. Data abstraction property is supported by c++. Data access is limited. It can be accessed by providing various visibility modes both for data and member functions. there by providing data security by data hiding Dynamic binding is supported by C++ 11..It supports all features of c language It can be called as an incremental version of c language Object Oriented Programming is programming paradigm that represents concepts as objects that has data fields and associated procedures known as methods. ❖ Concepts of OOP : C++ Classes and Objects : C++ is an object-oriented programming language. Everything in C++ is associated with classes and objects, along with its attributes and methods. For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as "class members". A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. Create an Object : In C++, an object is created from a class. We have already created the class named MyClass, so now we can use this to create objects. To create an object of MyClass, specify the class name, followed by the object name. To access the class attributes (myNum and myString), use the dot syntax (.) on the object: Example : class MyClass { // The class public: // Access specifier int myNum; // Attribute (int variable) string myString; // Attribute (string variable) }; int main() { MyClass myObj; // Create an object of MyClass // Access attributes and set values myObj.myNum = 15; myObj.myString = "Some text"; // Print attribute values cout char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double ○ It is possible for implicit conversions to lose information, signs can be lost (when signed is implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted to float). ○ Example : int x = 10; // integer x char y = 'a'; // character c // y implicitly converted to int. ASCII // value of 'a' is 97 x = x + y; 2. Explicit Type Conversion: This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type. In C++, it can be done by two ways: 1. Converting by assignment: This is done by explicitly defining the required type in front of the expression in parenthesis. This can be also considered as forceful casting. Syntax: (type) expression Example : double x = 1.2; // Explicit conversion from double to int int sum = (int)x + 1; 2. Conversion using Cast operator: A Cast operator is an unary operator which forces one data type to be converted into another data type. ❖ Enumeration : Enum in C++ is a data type that contains fixed set of constants. It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. The C++ enum constants are static and final implicitly. C++ Enums can be thought of as classes that have fixed set of constants. Example : #include using namespace std; enum week { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; int main() { weekday; day = Friday; cout