8 Objects and Classes
8 Objects and Classes
Objects and
OBJECTS Classes
Class
• A class is a way to bind data and associated function
together.
• A class is an expanded concept of a data structure,
instead of
holding only data , it can hold both data and function.
• The data is to be hidden from external use.
• Classes are generally declared using the
keyword class, with the following format:
class class_name
{
private:
variable declaration;
public:
function declaration;
…
• The body of the declaration can contain members
that can be either data or function declaration,
and optionally access specifier.
• private functionfunction
Only member of that can have access to private data
Object
• Once a class has been created, we can create
variable of that type(class type) by using following
syntax which is called object.
• Syntax:
class_name variable_name;
Ex:
student s;
• we can create any number of objects belonging
to that class by declaring more than one object in
one statement. This statement are written in
main().
• Ex:
class student
{
…
}s1,s2;
• Accessing class member:
Ex:
class item
{
int
num;
public:
void getdata(int
a,float b) void
putdata(void)
{
cout<<numb
er;
cout<<cost;
}
};
….
2) Outside the class definition:
• To write function we need to declare function
inside the class and definition(function body) is
written outside the class.
• The general form of a member function
definition:
return_type
class_name::function_name(arument)
{
function body
}
• The membership label class_name :: tells
the compiler that the function
function_Name belongs to the class
Ex:
….
void item::getdata(int a,
float b)
{
number=
a;
coat=b;
}
void
item::putdat
a(void)
{
cout<<“number
“<<number; cout<<
“cost ”<<cost;
Scope Resolution Operator(::)
• It is used to define a function outside a class.
#include<iostream>
using namespace std;
class A
{
public:
// Only declaration
void fun();
};
// Definition outside class using ::
void A::fun()
{
cout << "fun() called";
}
int main()
{
A a;
a.fun();
return 0;
}
Object passing as an argument:
class Example {
public:
int a;
// Driver Code
int main()
{
// Create objects
Example E1, E2;
return 0;
}
Output:
Initial Values Value of object 1:
50
& object 2: 100
New values Value of object 1: 50
& object 2: 150
Returning Object as argument
• Syntax:
object = return object_name;
#include <bits/stdc++.h>
using namespace std;
class Example {
public:
int a;
return 0;
}
Output
• If both objects are of the same type (that is, both are objects of the
same class), then one object may be assigned to another.
#include <iostream>
using namespace std;
class myclass {
int a, b;
public:
void setab(int i, int j) { a = i, b = j; }
void showab();
};
void myclass::showab()
{
cout << "a is " << a << '\n';
cout << "b is " << b << '\n';
}
int main()
{
myclass ob1, ob2;
ob1.setab(10, 20);
ob2.setab(0, 0);
cout << "ob1 before assignment: \n";
ob1.showab();
cout << "ob2 before assignment: \n";
ob2.showab();
cout << '\n';
ob2 = ob1; // assign ob1 to ob2
cout << "ob1 after assignment: \n";
ob1.showab();
cout << "ob2 after assignment: \n";
ob2.showab();
return 0;
}