Class and Objects
Class​
A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects.
data members -> storage -> variable
member function -> to perform operation -> function
int roll; //data members
string name;
void display() // member functions
{
information
}
How to create class?​
class <className>
{
public : <-//access specifiers
//data members
int roll;
// member functions
void show(){ }
};
Access Specifiers​
- Public
- Private
- Protected
class Student // create class
{
public:
int roll;
void show () {
cout<<"Your roll number is : "<<roll<<endl;
}
}
How to create object ?​
classname objectName;
class Student // create class
{
public:
int roll;
void show () {
cout<<"Your roll number is : "<<roll<<endl;
}
}
int main(){
Student s1, s2; //create object
s1.roll = 123;
s2.roll = 567;
s1.show();
s2.show();
}
Output :
Your roll number is : 123
Your roll number is : 567
Explanation :
Create a class Student, write access specifier public, declare 1 datamember roll.
create a show() function member and print Your roll number is :
and roll variable.
In main function create object of Student class s1 and s2.
pass value in object s1.roll = 123
pass value in object s2.roll = 567
using object call show() function
Example 1:
#include <iostream>
using namespace std;
class Student
{
public:
int age;
string name;
void getData()
{
cout<<"Enter Name :";
cin>>name;
cout<<"Enter Your age :";
cout>>age;
}
void showData()
{
cout<<"Name :"<<name<<"Age : "<<age;<<endl;
}
};
int main() {
Student s1;
s1.getData();
s2.getData();
s1.showData();
s2.showData();
return 0;
}
Output :
Enter Name : Ayesha
Enter Your age :20
Name : Ayesha Age : 20
Enter Name : Harshal
Enter Your age :20
Name : Harshal Age : 20
Explanation :
Create a class Student, write access specifier public, declare 2 datamember age and name.
create a getdata() function and print Enter name and age
create a showData() function and print name and variable, age and variable
In main function create object of Student class s1 and s2.
using object call getData() and showData()
Example 2:
#include <iostream>
using namespace std;
class Rectangle
{
public:
int length;
int breadth;
void getData()
{
cout<<"Enter Length and Breadth :";
cin>>length>>breadth;
cout<<endl;
}
void calculateArea()
{
int area = length * breadth;
cout<<"Area : "<<area;
cout<<endl;
}
};
int main() {
Rectangle r1, r2;
r1.getData();
r1.calculateArea();
r2.getData();
r2.calculateArea();
return 0;
}
Output :
Enter Length and Breadth : 20 10
Area : 200
Enter Length and Breadth :10 10
Area : 100
Explanation :
Create a class Rectangle, write access specifier public, declare 2 datamember length and breadth.
create member function calculateArea
declare area variable
and calulate length * breadth
and print area
.
In main function create object of Rectangle class r1 and r2.
using object call getData() and calculateArea()