

































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
Object-Oriented Programming - Features of C++ - I/O Operations, Data Types, Variables-Static, Constants-Pointers-Type Conversions – Conditional and looping statements – Arrays - C++ 11 features - Class and Objects, Abstraction and Encapsulation, Access Specifiers, Methods- UML Diagrams Introduction – Use Case Diagram - Class Diagram.
Typology: Study notes
1 / 41
This page cannot be seen from the preview
Don't miss anything!
Aims to implement real world entities like inheritance, hiding, polymorphism etc in programming Bind data and functions together Functions associated with the data object can only access this data, No other part of code can access Code reusable - easily maintain and modify existing code
It is the process of using an object oriented methodology to design a computing system or application Provides a system of interacting objects for the purpose of solving a software problem
S.No Procedure Oriented Programming/ Structure Object Oriented Programming
1. It follows top down approach.^ It follows bottom up approach.
Program is divided into small parts called functions
Program is divided into small parts called objects.
3. function is more important than data.^ data is more important than function
There is no access specifier in procedural programming.
have access specifiers like private, public, protected etc.
It is less secure than OOPs. Because no data hiding.
Data hiding is possible, hence more secure than procedural.
Data moves freely within the system from one function to another.
In OOP, objects can move and communicate with each other via member functions. Type
overloading is not possible. Overloading is possible in object oriented programming.
No inheritance. inheritance is present in object-oriented programming.
There is no code reusability. Adding new data and function in POP is not so easy.
It offers code reusability by using the feature of inheritance. OOP provides an easy way to add new data and function.
10. Does not Supports Polymorphism^ Supports Polymorphism 11. Does not Supports Virtual Functions^ Supports Virtual Functions
Examples: C, FORTRAN, Pascal, Basic etc.
Examples: C++, Java, Python, C# etc.
Student Name Roll no Mark Getmark() Get Result()
Car
Colour price
Getdata() Process()
4. Data abstraction: Data abstraction provides /exposes only essential features (like interface details) and hides background details (Implementation details).
Class use the concept of data abstraction so they are called abstract data type (ADT)
5.Polymorphism:
Polymorphism means the ability to take more than one form. For example, an operation have different behavior in different instances. The behavior depends upon the type of the data used in the operation.
Polymorphism comes from the Greek words “poly” and “morphism”. “poly” means many and “morphism” means form i.e.. many forms.
6.Inheritance:
Inheritance is the process of creating new classes by deriving the properties and characteristics from another existin class.
It helps realize the goal of constructing software from reusable parts, rather than hand coding every system from scratch. When the class child, inherits the class parent, the class child is referred to as derived class (sub class) and the class parent as a base class (super class). In this case, the class child has two parts:
Binding refers to linking of procedure call to the code to be executed at the run time.
8. Message passing:
Objects communicates with each other by sending and receiving information.
C++ is a general purpose object programming language developed by Bjarne Stroustrup in 1980s at Bell Labs.
C++ is a superset of c, i.e. All C programs are also C++ programs
BASIC STRUCTURE OF C++ LANGUAGE
#include
using namespace std;
int main() { cout << "Hello World" ;
return 0; }
SIMPLE C++ PROGRAM 2:
#include
C++ I/O operation uses the stream concept. Stream is the sequence of bytes or flow of data. It makes the performance fast. If bytes flow from main memory to device like printer, display screen, or a network connection, etc, this is called as output operation. If bytes flow from device like printer, display screen, or a network connection, etc to main memory, this is called as input operation.
Header File
Standard output stream (cout)
The cout is a predefined object of output stream (ostream class). It is connected with the standard output device, which is usually a display screen. The cout is used in conjunction with stream insertion operator (<<) to display the output on a console
Example: cout<<"Enter Rollno";
Cout<<”Enter Rollno”<<Enter Name”;
Standard input stream (cin)
The cin is a predefined object of input stream (istream class). It is connected with the standard input device, which is usually a keyboard. The cin is used in conjunction with stream extraction operator (>>) to read the input from a console.
Example: cin>>name;
Cin>>rollno>>name;
Standard end line (endl)
The endl is a predefined object of ostream class. It is used to insert a new line characters and flushes the stream.
Example: cout << " C++ Program"<<endl;
Note: We can use the << or >> operator multiple times in the same line. This is called cascading.
A data type is used to indicate the type of data value stored in a variable.
Types of data types:
Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. Primitive data types includes:
Primary data type Description Size
int (^) used to store whole numbers 4 bytes
Char
A single character can be defined as a character data type ndicated with single quotes (’a’, ’3’). 1 byte
Float
Floating point number represents a real number with 6 digits precision 4 bytes Double Floating precision.^ point^ number^ represents^14 digits^ of 8 bytes
bool Boolean or logical data type is a data type, having two values (usually denoted true and false), 1 byte
Void
Void data type has no values. This is usually used to specify the return type of functions. The type of the function said to be void when it does not return any value to the calling function. This is also used for declaring general purpose pointer called void pointer
Example: int i, j, k; char c, ch; float f, salary; double d;
Variables are declared at three basic places
Location
local variable a variable is declared inside a function
formal parameter a variable is declared in the definition of function parameters global variable variable is declared outside all functions
Initializing Variables
Static Initialization : Here, the variable is assigned a value in advance. This variable then acts as a constant.
Example:
Declaring the variable and then initializing it
int a;
a = 5;
Declaring and Initializing the variable together int a = 5;
Declaring multiple variables simultaneously and then initializing them separately
int a, b;
a = 5;
b = 10;
Declaring multiple variables simultaneously and then initializing them simultaneously
int a=5, b = 10, c = 20;
Dynamic Initialization : Here, the variable is assigned a value at the run time. The value of this variable can be altered every time the program is being run.
int a;
cout << "Enter the value of a"; cin >> a;
Static keyword has different meanings when used with different types. We can use static keyword with:
Static Variables: Variables in a method or function, Variables in a class Static Members of Class : Class objects and Functions in a class
A static variable:
When a variable is declared as static, space for it gets allocated for the lifetime of the program. Even if the function is called multiple times, space for the static variable is allocated only once The value of variable in the previous call gets carried through the next function call
Ex: static int count = 0;
Static Class objects and static member functions: Just like variables, objects and methods also when declared as static have a scope till the lifetime of program.
Static member functions: are allowed to access only the static data members or other static member functions , they can not access the non-static data members or member functions of the class.
Example Program: #include
// value is updated and will be carried to next function calls count++; }
int main() {
Representation: These numbers can be represented in either decimal notation or exponent notation (scientific notation). Decimal notation: 1234.56, 75.098, 0.0002, -0.00674 (valid notations)
Exponent or scientific notation: General form: Mantissa e exponent
Mantissa: It is a real number expressed in decimal notation or an integer notation. Exponent: It is an integer number with an optional plus (+) or minus (-) sign. E or e : The letter separating the mantissa and decimal part.
Ex: 0.00000000987 is equivalent to 9.87e-
Character constants:- Single character constants : It is character (or any symbol or digit) enclosed within single quotes. Ex: ‘a’. ‘1’. ‘*’ Every Character constants have integer values known as ASCII values. (ASCII stands for American Standard Code for Information Interchange)
String constants or string literal: String constant is a sequence of zero or more characters enclosed by double quotes. Example: “MRCET” “12345” “*)(&%”
Pointer is a derived data type. A pointer is a variable used to store the address of a memory cell. Pointer variable can point to another pointer(double pointer)
Pointer Declarations
datatype *ptr_var_name; ex: int *p1; // pointer to int char *p2; //pointer to char;
Initialization of ptr:
Can be initialized (assigned) with the address of another variable using & (address of ) operator. Ex: int *p1, a; P1=&a; Note : both the pointer and pointee should be of same data type.
Generic pointer:
Pointers of type void are known as generic pointers. It can address of any variable.
Accessing a variable using pointer:
Variable can be accessed using the Pointer variable with dereference operator (*).
Example:
#include
using namespace std;
int main()
{
int var1=11;
int var2=22;
int *ptr1, *ptr2;
ptr1 =&var1; //pointer points to var
ptr2 =&var2;
cout<<ptr1<<" "<<ptr2;
cout<<&ptr1<<" "<<&ptr2;
return 0;
}
Output : 11 22 0x7ffe76905210 0x7ffe
Accessing an array using pointer:
Arrays can be accessed using the pointer variables. Base address (starting address) of an array is assigned to a pointer variable. Pointer variable is incremented to access the subsequent array elements.
Example:
#include
using namespace std;
#include
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5}; int *ptr = &arr[0];
When a member function is called this pointer is automatically passed as an implicit argument. It is the pointer to the invoking object. Member function can identify the object that invoked the function. i.e member function can find the address of the object. This pointer can be used to access the data in the object it points to. Objects can be returned by reference using this pointer rather than creating a temporary object.
Note:
To understand ‘this’ pointer, it is important to know how objects look at functions and data members of a class.
Each object gets its own copy of data members and all objects share a single copy of member functions. Now, The compiler supplies an implicit pointer along with the names of the functions as ‘this’.
Example Program:
#include
using namespace std;
class Test { private: int x; public: void setX (int x) { // The 'this' pointer is used to retrieve the object's x // hidden by the local variable 'x' this->x = x; // x=x; / * use x=x; instead of this->x = x and check, output willbe zero */
}
void display() { cout << "x = " << x << endl; }
int main() { Test obj,obj1; int x = 20; int y=30; obj.setX(x); obj1.setX(y); obj.display(); obj1.display(); return 0; } O/p: x=20 x=
References:
A special type of variable is called as enumerated or reference variable is used to provide alias (alternative name) for previously defined variable.
Syntax: datatype &ref_name = var_name;
Eg: float total=100;
float &sum =total;
cout<<sum<<total; // both sum and total refers the same object in memory.
S.NO References Pointers 1 No null reference Pointers can be made null 2 Once a reference is initialized to an object it cannot be changed to refer another object
Pointers can be pointed to another object at any time
3 A reference must be initialized when it is created
Pointers can be initialized at any time.
Type conversion or typecasting of variables refers to changing a variable of one data type into another.
Implicit Type Conversion:
While type conversion is done implicitly, (automatic type conversion Done by the compiler on its own) All the data types of the variables are upgraded to the data type of the variable with largest data type. Example : int x = 10; char y = 'a'; // y implicitly converted to int. ASCII // value of 'a' is 97
// Explicit conversion from double to int int sum = (int) x + 1;
Example Program for type casting
#include
int main() { double x = 1.2;
// Explicit conversion from double to int int sum = (int)x + 1;
cout << "Sum = " << sum;
return 0; }
Class:
Template or blueprint that describes the object It is a collection of data and member functions that manipulate data. Memory is allocated at run time when object created It is also known as user defined data type or ADT(abstract data type) A class is declared by the keyword class. Syntax:-
class class_name { Access specifier : Variable declarations; Access specifier : function declarations; };
Example of a class in C++: class student { private : int roll; char name[30]; public:
void get_data()
cout<<”Enter roll number and name”: cin>>roll>>name; }
void put_data() { cout<<”Roll number:”<<roll<<endl; cout<<”Name :”<<name<<endl; } };
Object: Instance of a class is called object. Run time entity Has object name, data(attributes), Methods(functions)
Note: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, we need to create objects.
Syntax: class_name object_name; Ex: student s;
Accessing members :. dot operator is used to access members of class
Syntax: Object-name.function-name(actual arguments); Ex: s.get_data(); s.put_data();
int main() { student s; s.getdata(); s.putdata(); returm 0; }
Scope:- Visibility or availability of a variable/ function in a program is called as scope. There are two types of scope. i)Local scope ii)Global scope