c++day-4

17

Upload: eshamu

Post on 27-Oct-2014

102 views

Category:

Documents


0 download

DESCRIPTION

From these documents you will able to learn step by step, and you will be able to create projects as well as you can be good programmer or a good teacher or trainer on your filed

TRANSCRIPT

Page 1: C++Day-4
Page 2: C++Day-4

Session Objectives

Define Class & Objects

Private ,Public Access Specifier

How to Create a Object

How to call the member Function

Scope Resolution Operator

Page 3: C++Day-4

SYNTAX : class<classname>

{

private :

datatype member1;

datatype member2;

public :

<retu_type> func_name1(arguments);

<retu_type> func_name2(arguments);

};

TO CREATE A NEW CLASS

create a object from the main()functions Syntax:

classname<objectname1, objectname2,..>;

Page 4: C++Day-4

For Example : class emp

{

private:

char name[25]; //Member Data

int empno;

float salary;

public:

void get_data() // Member Function

{

---------

}

void show_details()

{

-------------

------------

};

Page 5: C++Day-4

objectname.func_name(arguments);

Page 6: C++Day-4

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;

}

Page 7: C++Day-4

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

Page 8: C++Day-4

#include<iostream.h>

#include<conio.h>

class smallobj

{

private :

int s;

public :

void setdata(int d)

{

s=d;

}

void showdata()

{

cout<<endl<<"the Data is

"<<s;

}

};

void main()

{

smallobj a1,a2;

clrscr();

a1.setdata(2500);

a2.setdata(4000);

a1.showdata();

a2.showdata();

getch();

}

Page 9: C++Day-4

Class & Objects Example #include<iostream.h>

#include<conio.h>

class distance

{

private:

int feet;

float inches;

public :

void setdist(int ft,float in)

{

feet=ft;

inches=in;

}

void getdist()

{

cout<<endl<<"Enter Feet and Inches

Value:";

cin>>feet>>inches;

}

void showdist()

{

cout<<endl<<feet<<endl<<inches;

}

};

void main()

{

distance dist1,dist2;

clrscr();

dist1.setdist(11,6.25);

dist2.getdist();

dist1.showdist();

dist2.showdist();

getch();

}

Page 10: C++Day-4

#include<iostream.h>

#include<conio.h>

const int MAX=100;

class stack

{

private:

int st[MAX];

int top;

public :

stack() {

top=0; }

void push(int v) {

st [++top]=v; }

int pop()

{

return st[top--];

}

};

void main()

{

stack s1;

s1.push(11);

s1.push(22);

cout<<" Ist Data :"<<s1.pop();

cout<<" IInd Data :"<<s1.pop();

s1.push(33);

s1.push(44);

s1.push(55);

s1.push(66);

cout<<" III Data :"<<s1.pop()<<endl;

cout<<" IV Data :"<<s1.pop()<<endl;

cout<<" V Data :"<<s1.pop()<<endl;

cout<<" VI Data :"<<s1.pop()<<endl;

getch();

}

Page 11: C++Day-4

ARRAY OF OBJECTS I #include<iostream.h>

const int MAX=100;

class Distance

{

private:

int feet;

float inches;

public :

void get()

{

cout<<"\n Enter Feet Value :";

cin>>feet;

cout<<"\n Enter Inche Value :";

cin>>inches;

}

void show()

{

cout<<feet<<"\t"<<inches<<endl;

}

};

void main()

{

Distance dist[MAX];

int n=0;

char ans;

cout<<endl;

do

{

cout<<"\n Enter Distance "<<n+1;

dist[n++].get();

cout<<"\n Another Data [y/n]?";

cin>>ans;

}while(ans != 'n');

for(int j=0;j<n;j++)

{

cout<<"Distance Number "<<j+1<<"

Is ";

dist[j].show();

}

}

Page 12: C++Day-4

ARRAY OF OBJECTS II #include<iostream.h>

#include<conio.h>

class circle {

private:

int area, radius;

public:

void getdata() {

cout<<"Enter the radius

value"<<endl;

cin>>radius;

}

void calculate() {

area=3.14*radius*radius;

}

void show() {

cout<<"the area is "<<area<<endl;

}

};

void main() {

int i;

circle e[5];

for(i=0;i<5;i++)

{

e[i].getdata();

e[i].calculate();

e[i].show(); }

getch(); }

Page 13: C++Day-4

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

Page 14: C++Day-4

Scope Resolution Operator (::)

The member function of

A program are defined outside

the boundary of the class using

the scope resolution operator(::) #include<iostream.h>

class circle {

private:

int radius;

float area;

public:

void getdata();

void get(int r);

void calculate();

void show();

};

void circle::getdata(){

cout<<"RADIUS"<<endl;

cin>>radius; }

void circle::get(int r) {

radius=r; }

void circle::calculate()

{

area=3.14*radius*radius;

}

void circle::show()

{

cout<<"Area"<<area;

}

void main() {

circle r;

r.getdata();

r.calculate();

r.show();

r.get(100);

r.calculate();

r.show();

}

RESULT :

ENTER THE RADIUS : 5

Area FOR 5=78.5

Area FOR 100=31400

Page 15: C++Day-4

Returning Values from class's

Memeber Function to main()

function

#include<iostream.h>

#include<conio.h>

class rectangle

{

private :

int length,breadth,area;

public :

void setdata(int a,int b)

{

length=a;

breadth=b;

}

int calculate()

{

return length*breadth;

}

};

void main()

{

rectangle r;

int area;

clrscr();

r.setdata(5,8);

area=r.calculate();

cout<<endl<<" Area is :"<<area;

getch();

}

Page 16: C++Day-4

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.

Page 17: C++Day-4

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?