Unit 1 C C Notes
Unit 1 C C Notes
Object-Oriented Programming
Object-oriented programming can be defined as a programming model which is based upon the
concept of objects. Objects contain data in the form of attributes and code in the form of methods.
Q2. Object-oriented programming – As the name suggests uses objects in programming. Object-
oriented programming aims to implement real-world entities like inheritance, hiding,
polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of the code can access this data except that
function.
There are some basic concepts that act as the building blocks of OOPs i.e.
1. Class
2. Objects
3. Encapsulation
4. Abstraction
5. Polymorphism
6. Inheritance
7. Dynamic Binding
8. Message Passing
C C++
• C was developed by Dennis Ritchie
• C++ was developed by Bjarne Stroustrup
between the year 1969 and 1973 at
in 1979.
AT&T Bell Labs.
• C does no support polymorphism,
• C++ supports polymorphism,
encapsulation, and inheritance which
encapsulation, and inheritance because it is
means that C does not support object
an object oriented programming language.
oriented programming.
• C is (mostly) a subset of C++. • C++ is (mostly) a superset of C.
• C++ is known as hybrid language because
• For the development of code, C
C++ supports both procedural and object
supports procedural programming.
oriented programming paradigms.
• Data and functions are separated in C
• Data and functions are encapsulated
because it is a procedural programming
together in form of an object in C++.
language.
• Data is hidden by the Encapsulation to
• C does not support information hiding. ensure that data structures and operators
are used as intended.
• Built-in & user-defined data types is
• Built-in data types is supported in C.
supported in C++.
• C is a function driven language because
• C++ is an object driven language because
C is a procedural programming
it is an object oriented programming.
language.
• Function and operator overloading is • Function and operator overloading is
not supported in C. supported by C++.
• C is a function-driven language. • C++ is an object-driven language
• Functions in C are not defined inside • Functions can be used inside a structure in
structures. C++.
• Namespace features are not present • Namespace is used by C++, which avoid
inside the C. name collisions.
• Standard IO header is stdio.h. • Standard IO header is iostream.h.
• Reference variables are not supported
• Reference variables are supported by C++.
by C.
• Virtual and friend functions are not • Virtual and friend functions are supported
supported by C. by C++.
• C does not support inheritance. • C++ supports inheritance.
• Instead of focusing on data, C focuses • C++ focuses on data instead of focusing
on method or process. on method or procedure.
• C provides malloc() and calloc()
• C++ provides new operator for memory
functions for dynamic memory
allocation and delete operator for memory
allocation, and free() for memory de-
de-allocation.
allocation.
• Direct support for exception handling
• Exception handling is supported by C++.
is not supported by C.
• scanf() and printf() functions are used • cin and cout are used for input/output in
for input/output in C. C++.
• C structures don’t have access
• C ++ structures have access modifiers.
modifiers.
• C follows the top-down approach • C++ follows the Bottom-up approach
• Strict type checking in done in C++. So
• There is no strict type checking in C many programs that run well in C
programming language. compiler will result in many warnings and
errors under C++ compiler.
• C does not support overloading • C++ does support overloading
• Type punning with unions is undefined
• Type punning with unions is allows
behavior (except in very specific
(C99 and later)
circumstances)
• Named initializers may appear out of • Named initializers must match the data
order layout of the struct
• File extension is “.cpp” or “.c++” or “.cc”
• File extension is “.c”
or “.cxx”
• Meta-programming: macros + • Meta-programming: templates (macros are
_Generic() still supported but discouraged)
• There are 32 keywords in the C • There are 97 keywords in the C++
cpp
#include <iostream>
using namespace std;
int main() {
int number;
// Input
cout << "Enter a number: ";
cin >> number;
// Output
cout << "You entered: " << number << endl;
return 0;
}
Literals are representations of specific values in a program. Constant qualifiers (const) are used
to create constants.
The const qualifier is used to create constants, making variables read-only after
initialization.
#include<iostream.h>
using namespace std;
int main() {
int variable = 5;
variable = constantValue; // Error, cannot modify a constant
return 0;
}
Comparison Operators
Comparison operators are used to compare two values (or variables). This is important in
programming, because it helps us to find answers and make decisions.
Logical Operators
As with comparison operators, you can also test for true (1) or false (0) values with logical
operators.
Logical operators are used to determine the logic between variables or values:
Now, both original and ref refer to the same memory location. Any changes made through one
variable will affect the other.
Example:
// Function declaration
void myFunction();
// Function definition
void myFunction() {
cout << "I just got executed!";
}
Example:
int main() {
display(5); // Outputs: Values: 5, 0
return 0;
}
void modifyValue(int x) {
x = x * 2;
}
int main() {
int number = 5;
modifyValue(number);
// 'number' remains 5, as it was passed by value
return 0;
}
Passing by Reference:
When you pass a parameter by reference, you are providing the function with direct access to the
memory location of the actual parameter. Any modifications made to the parameter inside the
function will affect the original variable.
int main() {
int number = 5;
modifyValue(number);
// 'number' is now 10, as it was passed by reference
return 0;
}
Passing by Pointer:
Passing by pointer involves passing the memory address of the actual parameter. Like passing by
reference, changes made through the pointer inside the function affect the original variable.
cpp
int main() {
int number = 5;
modifyValue(&number);
// 'number' is now 10, as it was passed by pointer
return 0;
}
Basic Syntax:
cpp
Here, the inline keyword suggests to the compiler that it should attempt to insert the function
code directly at the call site rather than generating a function call.
• Since the function code is inserted directly at the call site, there is no overhead
associated with function calls.
2. Performance Improvement:
• Inline functions can lead to performance improvements, especially for small,
frequently called functions.
3. Avoiding Function Call Stack Overhead:
• Inline functions avoid the stack overhead associated with function calls, making
them efficient for certain scenarios.
Example:
#include <iostream>
int main() {
int number = 5;
cout << "Square of " << number << " is: " << square(number) << endl;
return 0;
}
In this case, the int value is implicitly converted to a double without the need for explicit
casting.
• Functional Casting:
• C++ introduces functional casting operators, such as static_cast, dynamic_cast,
const_cast, and reinterpret_cast.
cpp
• static_cast is safer than C-style casting and is used for most type conversions.
3. class MyClass {
public:
operator double() const {
// Define how MyClass is converted to double
return 42.0;
}
};
MyClass myObject;
double convertedValue = static_cast<double>(myObject);
Example:
cpp
#include <iostream>
int main() {
int integerNumber = 42;
double doubleNumber = static_cast<double>(integerNumber); // Explicit
conversion
return 0;
}
derstanding type conversion is important for writing robust and efficient code, especially when
dealing with different data types in C++. It helps prevent unexpected behavior and ensures that data
is used appropriately in various contexts.
new Operator:
The new operator is used to allocate memory for a single object or an array. When allocating
memory for an array, the size is specified in square brackets.
cpp
delete Operator:
The delete operator is used to deallocate memory that was previously allocated using new. When
deleting an array, use square brackets to indicate that it's an array.
cpp
It's crucial to use delete (or delete[] for arrays) to free up the memory to prevent memory
leaks.
#include <iostream>
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
return 0;
}
This program dynamically allocates an array based on user input, initializes the array, and then
deallocates the memory when it's no longer needed.
vector as an Alternative:
In modern C++, using vector is often preferred over dynamic arrays because it provides
dynamic array functionality with additional safety features, automatic memory management, and is
part of the C++ Standard Library.
cpp
#include <iostream>
#include <vector>
int main() {
int size;
cout << "Enter the size of the vector: ";
return 0;
}
Using vector simplifies memory management and provides safety against common pitfalls
associated with raw dynamic arrays.