Sie sind auf Seite 1von 10

Classes and Object

C Structure
Consider following declaration:
struct student
{ new data type(structure name)
char name[10];
int rollno; structure member or elements
float marks;
};
Declaration of variable:
struct student A; // declaration in c
Accessing member variables:
A.rollno = 123;
A.marks = 455.8;
tatal = A.marks + 18;

C structure have some


limitation .
Specifying a class
Class is a way to bind data and its associated function
together.
Class specification has two parts:
1. Class declaration
2. Class function definition
The general form of class declaration is:
class class_name
{ abstract data type
private:
variable declaration; visibility labels
function declaration;
public: class body
variable declaration;
function declaration;
};
Variable of class
• called Objects
• Syntax…..
class_name variable_name;
• create memory space for an object.
• It is possible to declare an objects as…
class class_name
{
…….
…….
} object_name1,object_name2;
How to access class members?
Simply, by using following syntax:
object_name.function_name (arguments);
consider the following E.g……..
class employee
{
int emp_no;
float emp_sallary;
public:
int emp_phno;
void getdata (int a, float b, int c);
void putdata(void);
};
…………..
…………...
employee e1;
e1.emp_no;
e1.emp_phno; which is valied?
e1.getdata(12,135.5,563467);
………….
………….
How to define member functions?
By using following syntax:
return-type class_name :: function_name (argument declaration)
{ function body }
membership label scope resolution operator
For above E.g………
void employee :: getdata (int a, float b, int c);
{
emp_no = a;
emp_sallary = b;
emp_phno = c;
}
void employee :: putdata (void);
{
cout<<“Employee number is:”<<emp_no<<“\n”;
cout<<“Employee sallary is:”<<emp_sallary<<“\n”;
cout<<“Employee phone number is:”<<emp_phno<<“\n”;
}
# include<iostream.h>
# include<conio.h> void setdimension(int m,int n)
Class rectangle {
{ private: l = m;
int l, b; b = n;
public: }
void area() };
{ void main()
int a = l * b; {
cout<<“area is”<< a; clrscr();
} rectangle a,b;
void perimeter() a.setdimension(4,7);
{ b.setdimension(3,2);
int p = 2 * (l + b); a.area();
cout<<“perimeter b.parameter();
is”<< p; b.area();
} getch();
}
Design a class box conataining datamembers L, B, & H
and memberfunction setdimension & volume.
# include<iostream.h> {
Class box L = m;
{ B = n;
private: H = p;
int L,B,H; }};
public: Void main()
void volume() {
{ box a,b;
int v = L * B * H; a.setdimension(4,5,7);
cout<<“Volume is”<< v; b.setdimension(2,3,6);
} a.volume();
void setdimension(int m,int n, b.volume();
int p) }
Write program for………………..
1 Design a class worker, containing datamembers
wedges & wdays and memberfunction setdata &
payment.
2. Design a class account containing datamember
acc_no, balance and memberfunction open & interest.
3. Design a class account containing datamember
acc_no, balance and memberfunction open, deposit,
withdraw & showbalance.
4. Design a class worker, containing datamembers
wedges & wdays and memberfunction setdata,
payment, addwdays & change_wedges.

Das könnte Ihnen auch gefallen