unit - 1 C++-Final
unit - 1 C++-Final
Objects package data and the operations on them so that only the
operations are publicly accessible and internal details of the data
structures are hidden.
Abstraction:
Kinds Of Abstraction
1. Entity abstraction
2. Action abstraction
3. Virtual machine abstraction
4. Coincidental abstraction
Encapsulation :
Encapsulation is defined as the wrapping up of data and information
in a single unit. In Object Oriented Programming, Encapsulation is
defined as binding together the data and the functions that
manipulate them.
1. Data Protection:
Encapsulation protects its data members by keeping them as
private.
Access to and modification of these data members is restricted.
2. Information Hiding:
Encapsulation hides the internal implementation details of a class
from external code.
Only the public interface of the class is accessible, providing
abstraction.
Modularity:
Hierarchy
“IS–A” hierarchy
It defines the hierarchical relationship in inheritance.
From a super-class, a number of subclasses may be
derived which may again have subclasses and so on.
For example, if we derive a class Rose from a class
Flower, we can say that a rose “is–a” flower.
“PART–OF” hierarchy
It defines the hierarchical relationship in aggregation.
Here a class may be composed of other classes.
For example, a flower is composed of sepals, petals,
stamens, and carpel. It can be said that a petal is a
“part–of” flower.
Class
Class is a user-defined datatype that contain its own data
members and member functions.
The member functions and data members can be accessed with
the help of objects.
A class is used to organize information or data so a programmer
can reuse the elements in multiple instances.
Syntax of class
class <className>
{
public:
(public data member and member functions)
private:
(private data members and member functions)
};
Object
An object consists of
Example –
Data Members:
Member Functions:
Ex:
Class human
{
Private:
string name; // data member
int age; // data member
public:
void run (){ }; // member function
void eat (){ }; // member function
};
Access Specifiers:
Public:
All the class members declared under the public specifier will be
available to everyone.
The data members and member functions declared as public can
be accessed by other classes and functions too.
The public members of a class can be accessed from anywhere
in the program using the direct member access operator (.) with
the object of that class.
Private:
The class members declared as private can be accessed only by
the member functions inside the class.
They are not allowed to be accessed directly by any object or
function outside the class.
Only the member functions or the friend functions are allowed
to access the private data members of the class.
Static Member:
Classes can contain static member data and member functions.
When a data member is declared as static, only one copy of the
data is maintained for all objects of the class.
A declaration for a static member contains the keyword static.
The keyword static usually appears before other specifiers.
When a variable is declared as static, space for it gets
allocated for the lifetime of the program.
Example :
class X
{
public:
static int i;
};
Constructors:
Default constructor
1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. public:
6. Employee()
7. {
8. cout<<"Default Constructor Invoked"<<endl;
9. }
10. };
11. int main(void)
12. {
13. Employee e1; //creating an object of Employee
14. Employee e2;
15. return 0;
16. }
Output:
Default Constructor Invoked
Default Constructor Invoked
Parameterized Constructor
1. #include <iostream>
2. using namespace std;
3. class Employee {
4. public:
5. int id;//data member
6. string name;//data member
7. float salary;
8. Employee(int i, string n, float s)
9. {
10. id = i;
11. name = n;
12. salary = s;
13. }
14. void display()
15. {
16. cout<<id<<" "<<name<<" "<<salary<<endl;
17. }
18. };
19. int main(void) {
20. Employee e1 =Employee(101, "Sam", 890); //creating an obje
ct of Employee
21. Employee e2=Employee(102, "Ram", 590);
22. e1.display();
23. e2.display();
24. return 0;
25. }
Destructor in C++:
Example:
1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. public:
6. Employee()
7. {
8. cout<<"Constructor Invoked"<<endl;
9. }
10. ~Employee()
11. {
12. cout<<"Destructor Invoked"<<endl;
13. }
14. };
15. int main(void)
16. {
17. Employee e1; //creating an object of Employee
18. Employee e2; //creating an object of Employee
19. return 0;
20. }
class MyClass {
private:
int member1;
}
int main() {
MyClass obj;
include <iostream>
using namespace std;
class Distance {
private:
int meter;
// friend function
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Run Code
Output
Distance: 5
We can also use a friend Class in C++ using the friend keyword. For
example,
class ClassB;
class ClassA {
// ClassB is a friend class of ClassA
friend class ClassB;
}
class ClassB {
... .. ...
}
When a class is declared a friend class, all the member functions of
the friend class become friend functions.
Singleton Class:
Example:
#include <iostream>
class Singleton {
private:
4. Singleton() {} // Private constructor
public:
if (!instance) {
return instance;
}
};
Array of Objects:
Arrays are data structures of fixed size that are used for storing
and manipulating a collection of elements in C++.
Example:
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollno;
Student() {}
Student(string x, int y) {
name = x;
rollno = y;
}
void printDetails() {
cout << rollno << " - " << name << endl;
}
};
int main() {
//declare array with specific size
Student students[5];
//assign objects
students[0] = Student("Ram", 5);
students[1] = Student("Alex", 1);
students[2] = Student("Lesha", 4);
students[3] = Student("Emily", 3);
students[4] = Student("Anita", 2);
Pointer to objects
When declaring a pointer, the type of the pointer must match the
type of the variable it will be pointing to.
A pointer aims to point to a data type which may be int, character,
and double.
int *ptr;
double * ptr;
int *p1,*p2,i,j;
Example :
#include <iostream>
int main()
int x = 42;
std::cout << "The value of ptr is: " << ptr << std::endl;
std::cout << "The value pointed to by ptr is: " << *ptr << std::endl;
std:: cout << "The value of x is: " << x << std::endl;
return 0;
Output:
Reference Variable
use.
Initialize memory
Delete operator
Example:
#include <iostream>
int main()
{
int * p1 = &number;
int * p2;
*p2 = 99;
delete p2;
Output:
Number is :0x7ffd306f91d4
Namespaces:
Namespaces provide a method to avoid name conflicts in a project.
Namespaces in C++ are used to differentiate two or more variables,
functions, or classes having the same names.
namespace MyNamespace {