C++ UNIT-4 New
C++ UNIT-4 New
#include <iostream>
using namespace std;
// Global variable
int val = 100;
int main()
{
// Local variable
int val = 200;
return 0;
}
• A namespace is a declarative region that provides a scope to the identifiers (names
of the types, function, variables etc) inside it.
• Multiple namespace blocks with the same name are allowed. All declaration within
those blocks are declared in the named scope .
• A namespace definition begins with the keyword namespace followed by the
namespace name as follows:
• Namespace declarations appear only at global
scope.
• Namespace declarations can be nested within
another namespace.
• Namespace declarations don’t have access
specifiers. (Public or private)
• No need to give semicolon after the closing
brace of definition of namespace.
// Creating namespaces
#include <iostream>
using namespace std;
namespace ns1
{
int value() { return 5; }
}
namespace ns2
{
const double x = 100;
double value() { return 2*x; }
}
int main()
{
// Access value function within ns1
cout << ns1::value() << '\n';
return 0;
}
Identifiers
3. String Constant
If set of characters (such as alphabet or numeric or special symbol) are
enclosed between double cotes "" known as string character constant.
Example : “ abcd ”
“ tete3+-_=) ”
#include <iostream.h>
#include<conio.h>
void main () {
// Local variable declaration:
int x, y = 10;
x = (y < 10) ? 30 : 40;
cout << "value of x: " << x << endl;
}
Output value of x:40
Member (dot & arrow) Operators
• The . (dot) operator and the -> (arrow)
operator are used to reference individual
members of classes, structures, and unions.
• To access members of a structure, use the dot
operator. To access members of a structure
through a pointer, use the arrow operator.
Comma Operator( , )
The purpose of comma operator is to combined
together several expressions.
#include <iostream.h>
#include<conio.h>
void main()
{
int i, j;
j = 10;
i = (j++, j+100, 999+j);
cout << i;
}
pointer & Address operators(& *)
C++ provides two pointer operators, which are
C++ compiler convert all operands upto the type of the largest operand ,which is
called Type Promotion.
/ An example of implicit conversion
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
return 0;
}
Output
Sum=2
Functions
Depending on whether a function is
predefined or created by programmer, there
are two types of functions:
1. Library Function (Predefined function)
2. User-defined Function (created by
programmer like you as per need
User define function has three
component
• Function declaration or function Prototype.
• Function call
• Function definition or function defining
1. 1.Function declaration Syntax
return_type function_name (list of argument);
1.return_type: return type specify, which type of
value return by function, like int float, double
,char..etc .
* If function not return any value we take void
*By default return type is integer
2. function_name: Function name is valid
identifies, This name use in function call.
3. List of argument: these are place holder in
memory(RAM)
2. Function call Syntax
To execute the logic of function definition, the
user-defined function needs to be call
inside void main() function.
3. Function definition
Function definition has two part
function header
1. function header (which is same as function
declaration)
2. function body
1. #include < iostream.h>
2. #include<conio.h>
3. using namespace std;
4. int sumt (int x, int y); //declaring function
5. void main()
6. {
7. int a = 10;
8. int b = 20;
9. int d = sumt (a, b); // function call / control is transfer //to function
definition and a ,b value is transfer to x,y
10. cout << d;
11. }
12. int sumt (int x, int y) //defining function
13. {
14. int c;
15. c=x+y;
16. return (c);
17. }
exit() vs return()
Return : Transfer control to calling function.
Exit(): terminate the entire program and control
is transfer to OS itself.
cout<<"Value of b"<<b;
}
Inline function
C++ provides an inline functions to reduce the
function call overhead.
When the inline function is called whole code of the
inline function gets inserted or substituted at the
point of inline function call.
Code substitution is performed by the C++ compiler
at compile time.
Compiler may not perform inlining in
such circumstances like:
Function overloading allows you to have multiple functions with the same name but different parameter lists in the same scope. The compiler
determines which function to call based on the number and types of arguments passed.
#include <iostream>
using namespace std;
class Person {
public:
// Overloaded functions
void greet() {
cout << "Hello!" << endl;
}
void greet(string name) {
cout << "Hello, " << name << "!" << endl;
}
void greet(string name, int age) {
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
}
};
int main() {
Person p;
p.greet(); // Calls greet()
p.greet("Alice"); // Calls greet(string name)
p.greet("Alice", 30); // Calls greet(string name, int age)
return 0;
}
In this example, greet is overloaded. Depending on the arguments passed, different versions of greet are called.
2. Function Overriding
Function overriding allows a derived class to provide a specific implementation of a function that is already defined in its base class. This is
achieved with inheritance and requires the virtual keyword in the base class.
cpp
Copy code
#include <iostream>
using namespace std;
class Person {
public:
virtual void displayInfo() {
cout << "Person Info: This is a person." << endl;
}
};
int main() {
Person p;
Student s;
p.displayInfo(); // Calls Person's displayInfo()
s.displayInfo(); // Calls Student's displayInfo()
Person* personPtr = &s;
personPtr->displayInfo(); // Calls Student's displayInfo() due to polymorphism
return 0;
}
In this example, displayInfo is overridden in the Student class. The virtual keyword in the base class allows polymorphism, so calling
displayInfo on a Person pointer to a Student object will use the Student version.
3. Friend Function
A friend function is a function that is not a member of a class but has access to its private and protected members. You declare it with the
friend keyword inside the class.
#include <iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
Person(string n, int a) : name(n), age(a) {}
// Declaring a friend function
friend void showPersonInfo(const Person &p);
};
int main() {
Person p("Alice", 30);
showPersonInfo(p); // Calls the friend function
return 0;
}
In this example, showPersonInfo is a friend function of the Person class. It has access to Person's private members name and age, even though
it is not a member function. This is useful when you want certain external functions to access private data directly.
Summary