Beruflich Dokumente
Kultur Dokumente
Data Data
Communication
Functions Functions
Public
Object C
Functions
Data
Operation
Operation
Example: Student Object
Enroll()
st_name
st_id
Performa Displayinfo()
branch
nce()
semester
Result()
Class
• Class is a collection of similar objects.
Class
classes
class item
{
int number; // variables declaration
float cost; // private by default
Public:
void getdata(int a, float b); // functions declaration
void putdata(void); // using prototype
}; // ends with semicolon
• Attributes also called data members
– Because they hold information.
• Information hiding
– To prevent the internal representation from direct access
from outside the class
• Access Specifiers
– public
• may be accessible from anywhere within a program
– private
• may be accessed only by the member functions, and friends of this
class
– protected
• acts as public for derived classes
• behaves as private for the rest of the program
19
#include<iostream.h> int main( )
using namespace std; {
class smallobj smallobj s1,s2;
{ s1.setdata(1066);
int data; s2.setdata(1776);
public:
void setdata(int d); s1.showdata();
{ data = d; } s2.showdata();
void showdata( ); return(0);
{ cout<<data; } }
};
#include<iostream.h> int main( )
using namespace std; {
class part part part1;
{ part1.setpart(624,200);
int modelno;
part2.setpart(177,300);
float cost;
public:
void setpart(int mn,float c); part1.showpart();
{ modelno = mn; part2.showpart();
cost = c; } return(0);
void showpart( ); }
{ cout<<modelno;
cout<<cost; }
};
#include<iostream.h>
int main( )
using namespace std;
{
class distance distance dist1,dist2;
{ dist1.setdist(11,6.2);
int feet;
float inches; dist2.getdist( );
public:
void setdist(int ft,float in); cout<<dist1.showdist();
{ feet = ft;
inches = in; } cout<<dist2.showdist();
return(0);
void getdist( ); }
{ cin>>feet;
cint>>inches; }
void showdist()
{ cout<<feet<<inces; }
};
Another Example
#include <iostream.h> // member function definitions
int main(void) {
circle c; // an object of circle class
c.store(5.0);
cout << "The area of circle c is " << c.area() << endl;
c.display();
24
}