Classes and Objects
Introduction
• Just an extension of idea of structures in C
• A new way of creating and implementing user-
defined data type
Extensions to Structures
• In C++, structures can have both variables and functions
struct student
{
char name[20];
int rollno;
char grade[2];
};
struct student krishna; //Allowed in C
student krishna; //Error in C but allowed in C++
• In C++, structure names are stand-alone and can be used like any other type
names
• All these extensions are incorporated in a user defined type called class in C++
• Class is a specially designed data type in C++
• Structure are normally used for holding the data and classes for holding both data
and function
Creating classes
• A class is a way to bind data and its associated
functions together
• Class allows data and functions hiding
• When defining a class, we are creating a new
abstract data type
• A class specifications has:
– Class declaration
– Class function definitions
Creating classes
keyword
visibility labels
class class_name data members
{
private:
variable declarations;
class members
function declarations;
public:
variable declarations;
class members
function declarations;
};
Member functions
• The binding of data and functions together into a single class-
type variable is referred to as encapsulation
Class: STUDENT
Example DATA
roll_number
class student marks
{ …….
int roll_number; FUNCTIONS
insertdata()
float marks;
displaydata()
public: …….
void insertdata(int a, float b);
void displaydata(void); STUDENT
};
inserttdata()
displaydata()
……….
Accessing Objects
• Once class has been declared, we can declare
variable of a type by using the class name
student a; //Creates memory for a
• The class variable is known as object. Therefore, a is
an object of type student
student a, b, c ; //More than one object
declaration
class student
{
………
……..
……..
}a,b,c;
Accessing Class Members
• The private data of a class can be accessed only
through the member functions of that class
• The main() function can not contain statements that
access roll and marks directly
Object-name.function-name(actual-arguments);
• Example
a.insertdata(10, 85.4);
a.displaydata();
insertdata(10, 85.4); // Illegal. Member functions can be invoked only by using object
a.roll_number=10; // Roll number is declared private, so illegal
Accessing Class Members
• Objects communicate by sending and receiving
messages a.displaydata(); sends a message to object a
requesting it to display its contents
• A variable declared as public can be accessed by the
objects directly class
{
xyz
int x;
int y;
public:
int z;
};
…….
…….
xyz p;
p.x=10; //Error
p.z=100; //Correct, z is public
Defining Member Functions
• Two ways:
– Outside the class definition
– Inside the class definition
Defining Member Functions
• Outside the class definition
– Member functions defined inside a class have to be defined separately
outside the class
– The difference between a member function and a normal function is that
a member function incorporates a membership ‘identity label’ in the
header
– This ‘label’ tells the compiler which class the function belongs to
return-type class-name::function-name (argument declaration)
{
Function body
}
– The membership label class-name:: tells the compiler that the function-
name belongs to the class-name
Example
void student::insertdata(int a, float b)
{
roll_number = a;
marks = b
}
void student::displaydata(void)
{
cout<<“Roll Number: ”<<roll_number<<“\n”;
cout<<“Marks: marks: ”<<marks<<“\n”;
}
• Characteristics of Member functions
– Different classes can use same function name. ‘Membership label’ resolves their scope
– Member functions can access private data of the class. A non-member function can’t do so
(Exception: friend function)
– A member function can call another member function directly, without using the dot
operator.
Defining Member Functions
• Inside the class definition
– Replace function declaration by actual function definition inside the
class
class student
{
int roll_number;
float marks;
public:
void insertdata(int a, float b); //declaration
//inline function
void displaydata(void) //definition inside the class
{
cout<<roll_number<<“\n”;
cout<<marks<<“\n”;
}
};
An Example
class student
{
int roll_number;
float marks;
public:
void insertdata(int a,float b);
void displaydata(void)
{
cout<<"Roll Number: "<<roll_number<<"\n";
cout<<"Marks: "<<marks<<"\n\n";
}
};
An Example (Contd..)
void student::insertdata(int a, float b)
{
roll_number = a;
marks = b;
}
An Example (Contd..)
int main()
{
int student_roll;
float student_marks;
student x; //Derive object x from student class
cout<<"Enter Roll Number:\n";
cin>>student_roll;
cout<<"Enter Marks:\n";
cin>>student_marks;
cout<<“Student info:\n";
x.insertdata(student_roll, student_marks); //Call member functions of class student
x.displaydata();
return 0;
}
An Example2
class student
{
char name[20];
int roll_number;
float marks;
public:
void insertdata(char a[], int b,float c);
void displaydata(void)
{
cout<<"Name: "<<name<<"\n";
cout<<"Roll Number: "<<roll_number<<"\n";
cout<<"Marks: "<<marks<<"\n\n\n";
}
};
An Example2 (Contd..)
void student::insertdata(char a[], int b, float c)
{
for(int i=0;i<20;i++)
{
name[i] = a[i];
}
roll_number = b;
marks = c;
}
An Example2 (Contd..)
int main()
{
char student_name[20];
int student_roll;
float student_marks;
student x; //Derive object x from student class
cout<<"Enter name:\n";
cin>>student_name;
cout<<"Enter Roll Number:\n";
cin>>student_roll;
cout<<"Enter Marks:\n";
cin>>student_marks;
cout<<“Student info:\n";
x.insertdata(student_name, student_roll, student_marks); //Call member
x.displaydata(); // functions of class student
return 0;
}
Static member function
A static member function of a class in C++ (and many
other object-oriented languages) is a function that:
• Belongs to the class itself rather than any specific
object of the class.
• Can be called without creating an object of the class.
• Cannot access non-static members (variables or
functions) of the class directly, because it does not
operate on a specific instance (object) of the class.
Static member function
Syntax:
class MyClass {
public:
static void myStaticFunction();
};
Call
MyClass::myStaticFunction(); // No need to create an object
Example
#include <iostream>
using namespace std;
class Counter {
private:
static int count; // Static member variable
public:
static void increment() {
count++;
cout << "Count: " << count << endl;
}
};
int Counter::count = 0; // Definition of static member variable
int main() {
Counter::increment(); // Output: Count: 1
Counter::increment(); // Output: Count: 2
return 0;
}