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

C Day-4

The document provides an overview of classes and objects in programming, including definitions, access specifiers, and how to create and use objects. It includes code examples demonstrating the creation of classes, member functions, and the use of the scope resolution operator. Additionally, it discusses data abstraction and presents exercises related to the topic.

Uploaded by

beastvijay31180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views17 pages

C Day-4

The document provides an overview of classes and objects in programming, including definitions, access specifiers, and how to create and use objects. It includes code examples demonstrating the creation of classes, member functions, and the use of the scope resolution operator. Additionally, it discusses data abstraction and presents exercises related to the topic.

Uploaded by

beastvijay31180
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Session Objectives

Define Class & Objects

Private ,Public Access Specifier

How to Create a Object


How to call the member Function
Scope Resolution Operator
TO CREATE A NEW CLASS
SYNTAX :
class<classname>
{
private :
datatype member1;
datatype member2;
public :
<retu_type> func_name1(arguments);
<retu_type> func_name2(arguments);
};

create a object from the main()functions


Syntax:
classname<objectname1, objectname2,..>;
For Example :
class emp
{
private:
char name[25]; //Member Data
int empno;
float salary;
public:
void get_data() // Member Function
{
---------
}
void show_details()
{
-------------
------------
};
objectname.func_name(arguments);
Write a Program to calculate the area of a circle
using class & Object
#include<iostream.h>
#include<conio.h>
class raji
{
private:
int radius;
float area;
public:
void getdata()
{
cout<<"Enter the radius"<<endl;
cin>>radius;
}
void calculate()
{
area=3.14*radius*radius;
}
void showdata()
{
cout<<"The area of the circle is "<<area<<endl;

}
};
void main()
{
raji r;
r.getdata();
r.calculate();
r.showdata();
getch();
}

Enter the Radius : 2


The area of the circle is
#include<iostream.h> void main()
#include<conio.h> {
class smallobj smallobj a1,a2;
{ clrscr();
private : a1.setdata(2500);
int s;
a2.setdata(4000);
public :
void setdata(int d)
a1.showdata();
{ a2.showdata();
s=d; getch();
} }
void showdata()
{
cout<<endl<<"the Data is
"<<s;
}
};
Class & Objects Example void showdist()
#include<iostream.h> {
#include<conio.h> cout<<endl<<feet<<endl<<inches;
class distance }
{ };
private:
int feet; void main()
float inches; {
public : distance dist1,dist2;
void setdist(int ft,float in) clrscr();
{ dist1.setdist(11,6.25);
feet=ft; dist2.getdist();
inches=in; dist1.showdist();
} dist2.showdist();
getch();
void getdist() }
{
cout<<endl<<"Enter Feet and Inches
Value:";
cin>>feet>>inches;
}
#include<iostream.h> void main()
#include<conio.h> {
const int MAX=100; stack s1;
class stack s1.push(11);
{ s1.push(22);
private: cout<<" Ist Data :"<<s1.pop();
int st[MAX]; cout<<" IInd Data :"<<s1.pop();
int top; s1.push(33);
public : s1.push(44);
stack() { s1.push(55);
top=0; } s1.push(66);
void push(int v) { cout<<" III Data :"<<s1.pop()<<endl;
st [++top]=v; } cout<<" IV Data :"<<s1.pop()<<endl;
int pop() cout<<" V Data :"<<s1.pop()<<endl;
{ cout<<" VI Data :"<<s1.pop()<<endl;
return st[top--]; getch();
} }
};
ARRAY OF OBJECTS I void main()
#include<iostream.h> {
const int MAX=100; Distance dist[MAX];
class Distance int n=0;
{ char ans;
private: cout<<endl;
int feet; do
float inches; {
public : cout<<"\n Enter Distance "<<n+1;
void get() dist[n++].get();
{ cout<<"\n Another Data [y/n]?";
cout<<"\n Enter Feet Value :"; cin>>ans;
cin>>feet; }while(ans != 'n');
cout<<"\n Enter Inche Value :";
cin>>inches; for(int j=0;j<n;j++)
} {
void show() cout<<"Distance Number "<<j+1<<"
{ Is ";
cout<<feet<<"\ dist[j].show();
t"<<inches<<endl; }
} }
};
ARRAY OF OBJECTS II
#include<iostream.h> void calculate() {
area=3.14*radius*radius;
#include<conio.h> }
class circle { void show() {
cout<<"the area is "<<area<<endl;
private: }
int area, radius; };
void main() {
public: int i;
void getdata() { circle e[5];
for(i=0;i<5;i++)
cout<<"Enter the radius {
value"<<endl; e[i].getdata();
cin>>radius; e[i].calculate();
e[i].show(); }
} getch(); }
Output
Enter the radius value 1
The area is 3
Enter the radius value 2
The area is 12
Enter the radius value 3
The area is 28
Enter the radius value 4
The area is 50
Enter the radius value 4
The area is 50
Scope Resolution Operator (::) void circle::calculate()
The member function of {
A program are defined outside area=3.14*radius*radius;
}
the boundary of the class using
void circle::show()
the scope resolution operator(::)
{
#include<iostream.h>
class circle { cout<<"Area"<<area;
private: }
int radius; void main() {
float area; circle r;
public: r.getdata();
void getdata(); r.calculate();
void get(int r); r.show();
void calculate(); r.get(100);
void show(); r.calculate();
}; r.show();
void circle::getdata(){ }
cout<<"RADIUS"<<endl; RESULT :
cin>>radius; }
ENTER THE RADIUS : 5
void circle::get(int r) {
radius=r; } Area FOR 5=78.5
Returning Values from class's int calculate()
Memeber Function to main() {
return length*breadth;
function }
};
#include<iostream.h> void main()
#include<conio.h> {
class rectangle rectangle r;
{ int area;
private : clrscr();
int length,breadth,area; r.setdata(5,8);
public : area=r.calculate();
void setdata(int a,int b) cout<<endl<<" Area is :"<<area;
{ getch();
length=a; }
breadth=b;
}
Each class specification starts with the keyword “class”

The Class Specification must always end with a semicolon (;)

Data abstraction is the ability to create user-defined data


types for modeling real world objects using built-in data types.

Classes are used for data abstraction by hiding the


implementation of a type in the “private” part.
EXERCISES

1. Describe the use of Array of Objects?

2. List the various applications of Scope Resloution Operator?

3. Explain briefly Classes & Objects?

4. Describe the Array as Class member Data?

You might also like