0% found this document useful (0 votes)
7 views16 pages

C++ Classes and Objects Explained

his document is a class notes. It explains about the class and objects of OOPs. It is helpful for the students.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

C++ Classes and Objects Explained

his document is a class notes. It explains about the class and objects of OOPs. It is helpful for the students.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Objects and Classes in C++

Classes and Objects:


Classes in C++
• It is a collection of data and member functions that manipulate data.
• The data components of class are called data members and functions that
manipulate the data are called member functions.
• It can also called as blue print or prototype that defines the variables and
functions common to all objects of certain kind.
• It is also known as user defined data type or ADT(abstract data type)
• A class is declared by the keyword class.
Syntax
class classname
{
Access specifier:
Variable declarations;
Access specifier:
function declarations;
};

Access Control:
Access specifier or access modifiers are the labels that specify type of access given to
members of a class. These are used for data hiding. These are also called as visibility modes.
There are three types of access specifiers
[Link]
[Link]
[Link]
[Link]:
If the data members are declared as private access then they cannot be accessed from
other functions outside the class. It can only be accessed by the functions declared
within the class. It is declared by the key word private .
[Link]:
If the data members are declared public access then they can be accessed from other
functions out side the class. It is declared by the key word public .
[Link]: The access level of protected declaration lies between public and private.
This access specifier is used at the time of inheritance
Note:-
If no access specifier is specified then it is treated by default as private
Example:
class student
{
private :
int roll;
char name[30];
public:
void get_data()
{
cout<<”Enter roll number and name”:
cin>>roll>>name;
}
void put_data()
{
cout<<”Roll number:”<<roll<<endl;
cout<<”Name :”<<name<<endl;
}
};
Object
An Object can be defined as an entity or in other words, anything that exists physically
in the world is called an object.
It can represent a person, a place, a table, etc.
• Object is a run-time entity. Example: an item, a place, a bank account, person,
student, etc.,
• Object is treated as real world objects.
• It interacts by sending messages to one another.
• Each object contains data and code to manipulate that data.
Example:
Object : STUDENT
Data : Roll number, Name, Marks etc.
Functions: Total, Average, Result etc,
Object:-Instance of a class is called object.
Syntax:
class_name object_name;
Ex:
student s;
Accessing members:-dot operator is used to access members of class
[Link]-name(actual arguments);
Ex:
s.get_data();
s.put_data();
Note:
[Link] the access specifier is not specified in the class the default access specifier is private
[Link] member functions are to be declared as public if not they are not accessible outside the
class.
Declaring Object:
Instance of a class is called as object.
Syntax:
Class_name object name;
Example:
student s;
in the above example s is the object. It is a real time entity that can be used
Write a program to read data of a student
#include<iostream>
class student
{
private:
int roll;
char name[20];
public:
void getdata()
{
cout<<”Enter Roll number:”;
cin>>roll;
cout<<”Enter Name:”;
cin>>name;
}
void putdata()
{
cout<<”Roll no:”<<roll<<endl;
cout<<Name:”<<name<<endl;
}
};
int main()
{
student s;
[Link]();
[Link]();
return 0;
}
Defining Member Function
Class Scope:
Scope resolution operator(::) is used to define a function outside a class.
#include <iostream>
class sample
{
public:
void output(); //function declaration
};
// function definition outside the
class void sample::output()
{
cout << "Function defined outside the class.\n";
};
int main()
{
sample obj;
[Link]();
return 0;
}
Write a program to find area of rectangle
#include<iostream.h>
class rectangle
{
int L,B;
public:
void get_data();
void area();
};
void rectangle::get_data()
{
cout<<”Enter Length of rectangle”;
cin>>L;
cout<<”Enter breadth of rectangle”;
cin>>B;
}
int rectangle::area()
{
return L*B;
}
int main()
{
rectangle r;
r.get_data();
cout<<”Area of rectangle is”<<[Link]();
return 0;
}
Static Member Functions
A Member Functions that is declared as static has the following properties.
• A static function can have access to only other static members(functions or
variables) declared in the same class.
• A static member functions can be called using the class name (instead of its
objects)as follows
class_name : : function_name;
#include <iostream>
using namespace std;
class Account {
public:
int accno; //data member (also instance variable)
string name; //data member(also instance variable)
static float rateOfInterest;
Account(int accno, string name)
{
this->accno = accno;
this->name = name;
}
void display()
{
cout<<accno<< "<<name<< " "<<rateOfInterest<<endl;
}
};
float Account::rateOfInterest=6.5;
int main(void)
{
Account a1 =Account(201, "Sanjay"); //creating an object of Employee
Account a2=Account(202, "Nakul"); //creating an object of Employee
[Link]();
[Link]();
return 0;
}

You might also like