BCAMJ23402DS using C++ Object and Classes Unit 1
BCAMJ23402DS using C++ Object and Classes Unit 1
BCAMJ23402
Unit 1 [Object and Classes]
C++ Object
In C++, Object is a real-world entity, for example, chair, car, pen, mobile, laptop etc. In other
words, object is an entity that has state and behaviour. Here, state means data and behaviour
means functionality.
Object is a runtime entity; it is created at runtime. Object is an instance of a class. All the
members of the class can be accessed through object. Let's see an example to create object of
student class using s1 as the reference variable.
In this example, Student is the type and s1 is the reference variable that refers to the instance
of Student class.
C++ Class
In C++, object is a group of similar objects. It is a template from which objects are created. It
can have fields, methods, constructors etc.
Let's see an example of C++ class that has three fields only.
1. class Student
2. {
3. public:
4. int id; //field or data member
5. float salary; //field or data member
6. String name;//field or data member
7. };
2
17. }
18. };
19. int main(void) {
20. Employee e1; //creating an object of Employee
21. Employee e2; //creating an object of Employee
22. e1.insert(201, "Sonoo",990000);
23. e2.insert(202, "Nakul", 29000);
24. e1.display();
25. e2.display();
26. return 0;
27. }
1) To access a global variable when there is a local variable with same name:
// C++ program to show that we can access a global variable
// using scope resolution operator :: when there is a local
// variable with same name
3
#include<iostream>
using namespace std;
int x; // Global x
int main()
{
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
Output:
Value of global x is 0
Value of local x is 10
Access Specifiers
4
o Public: When the member is declared as public, it is accessible to all the functions of
the program. Members declared public can be accessed from anywhere (inside or
outside the class). It allows unrestricted access.
o Private: When the member is declared as private, it is accessible within the class only.
Members declared private can only be accessed inside the class. They cannot be
accessed directly from outside the class.
o Protected: When the member is declared as protected, it is accessible within its own
class as well as the class immediately derived from it. Members declared protected act
like private but can be accessed in derived (child) classes. Used mainly for
inheritance.
Comparison Table
Access Accessible Inside Accessible Outside Accessible in Derived
Specifier Class Class Class
public ✅ Yes ✅ Yes ✅ Yes
private ✅ Yes ❌ No ❌ No
protected ✅ Yes ❌ No ✅ Yes
Constructor
In C++, constructor is a special method which is invoked automatically at the time of object
creation. It is used to initialize the data members of new object generally. The constructor in
C++ has the same name as class.
5
There can be two types of constructors in C++.
o Default constructor
o Parameterized constructor
Default Constructor
A constructor which has no argument is known as default constructor. It is invoked at the
time of creating object.
Let's see the simple example of C++ 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. }
#include<iostream>
using namespace std;
class Employee
{
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void)
{
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2(102, "Nakul", 59000);
6
e1.display();
e2.display();
return 0;
}
Destructor
A destructor works opposite to constructor; it destructs the objects of classes. It can be defined
only once in a class. Like constructors, it is invoked automatically. A destructor is defined like
constructor. It must have same name as class. But it is prefixed with a tilde sign (~).
Output:
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked
Copy Constructor
A Copy constructor is an overloaded constructor used to declare and initialize an object from
another object.
In the above case, copy constructor can be called in the following ways:
Output:
20
In C++ we can pass class’s objects as arguments and also return them from a function the
same way we pass and return other variables. No special keyword or header file is required
to do so.
Passing an Object as argument
8
To pass an object as an argument we write the object name as the argument while calling
the function the same way we do it for other variables.
Syntax:
function_name(object_name);
Example: In this Example there is a class which has an integer variable ‘a’ and a function
‘add’ which takes an object as argument. The function is called by one object and takes
another as an argument. Inside the function, the integer value of the argument object is added
to that on which the ‘add’ function is called. In this method, we can pass objects as an
argument and alter them.
// C++ program to show passing
// of objects to a function
#include <iostream>
using namespace std;
class Example {
public:
int a;
// This function will take
// an object as an argument
void add(Example E)
{
a = a + E.a;
}
};
// Driver Code
int main()
{
// Create objects
Example E1, E2;
// Values are initialized for both objects
E1.a = 50;
E2.a = 100;
cout << "Initial Values \n";
cout << "Value of object 1: " << E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";
// Passing object as an argument
// to function add()
E2.add(E1);
// Changed values after passing
// object as argument
cout << "New values \n";
cout << "Value of object 1: " << E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";
return 0;
}
Output
Initial Values
Value of object 1: 50
9
& object 2: 100
New values
Value of object 1: 50
& object 2: 150
10
<< "\n";
// Passing object as an argument
// to function add()
E3 = E3.add(E1, E2);
// Changed values after
// passing object as an argument
cout << "New values \n";
cout << "Value of object 1: " << E1.a
<< ", \nobject 2: " << E2.a
<< ", \nobject 3: " << E3.a
<< "\n";
return 0;
}
Output
Initial Values
Value of object 1: 50,
object 2: 100,
object 3: 0
New values
Value of object 1: 50,
object 2: 100,
object 3: 150
#include <iostream>
using namespace std;
class Student {
public:
string name;
int rollNo;
static int count; // Declaration of static data member
// Constructor to increment count when a new student is created
Student(string n, int r)
{
name = n;
rollNo = r;
count++; // Increments for each new object
}
void display()
11
{
cout << "Name: " << name << ", Roll No: " << rollNo << endl;
}
};
int main() {
// Shape s; // ❌ Error: Cannot instantiate abstract class
Circle c(5.0);
Rectangle r(4.0, 6.0);
c.area(); // Calls overridden function in Circle
r.area(); // Calls overridden function in Rectangle
return 0;
}
Output:
Circle Area: 78.5
Rectangle Area: 24
In the above declaration, the friend function is preceded by the keyword friend. The function
can be defined anywhere in the program like a normal C++ function. The function definition
does not use either the keyword friend or scope resolution operator.
15
13. };
14. class B
15. {
16. int y;
17. public:
18. void setdata(int i)
19. {
20. y=i;
21. }
22. friend void min(A,B); // friend function
23. };
24. void min(A a,B b)
25. {
26. if(a.x<=b.y)
27. scout << a.x << endl;
28. else
29. cout << b.y << endl;
30. }
31. int main()
32. {
33. A a;
34. B b;
35. a.setdata(10);
36. b.setdata(20);
37. min(a,b);
38. return 0;
39. }
Output:
10
In the above example, min() function is friendly to two classes, i.e., the min() function can
access the private members of both the classes A and B.
16
}
};
int main()
{
A a;
B b;
b.display(a);
return 0;
}
Output:
value of x is : 5
In the above example, class B is declared as a friend inside the class A. Therefore, B is a
friend of class A. Class B can access the private members of class A.
Source
1. C++ Programming Language - GeeksforGeeks
2. C++ Tutorial | Learn C++
3. C++ Tutorial | Learn C++ Programming for Beginners - Tpoint Tech
17