0% found this document useful (0 votes)
17 views12 pages

Structure and Design of A Class

Uploaded by

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

Structure and Design of A Class

Uploaded by

mainaephraim356
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

The Structure and Design of a class

A class has two parts:


a.The public portion
b.The private portion
The public portion consists of those members
either data or those functions accessible outside the
class.
The private portion consists of member
functions not accessible outside the class. They can
only be accessed within the class OBJECT.
 A class definition begins with the keyword
class.
 The body of the class is contained within a
set of braces, { } ; (notice the semi-colon
class class_name
Any valid
{ identifier
….
Class body
…. (data
…. member +
methods)
};
Class terminator
(semi colon)
•An example of a class
1. class Circle NAME OF THE CLASS
No need for others
2. { classes to access and
retrieve its value
3. private: directly. The class
methods are
4. double radius;// data(private) responsible for that
only.
5. public:
6. voidsetRadius(double r); They are accessible
from outside the
7. doublegetDiameter(); } class, and they can
access the member
8. doublegetArea(); (radius)(PUBLIC
DATA MEMBERS)
9. doublegetCircumference();
Class terminator(semi
colon)
 The key words private and public should be followed by a
full colon.
 Outside the class, we create objects of particular class.
These objects accesses its attributes defined in the class via
a function. E,g
1. int main()
2. {
Object of the previous
3. Circle firstprogram; class circle define in the

4. firstprogram.wheel(23,45,90); main functon

5. firstprogram.display();
6. return 0;
7.}
•firstprogram is declared as an object of the class circle.
•A class is an actual representation of an ADT.
•It therefore provides implementation details for the
used data structure and operations. We play with the
ADT Integer and design our own class for it:
1.class Integer {
2.attributes:
3.int a;
4.methods:
5.setValue(int n)
6.addValue(Integer j)
7.};
•In this notation class {…} denotes the
definition of a class. Enclosed in the curly
brackets are two sections attributes: and
methods: which define the implementation of
the data structure and operations of the
corresponding ADT. Again we distinguish the
two levels with different terms:
Attributes which are elements of the data
structure at the ADT level.
Methods are the implementation of the
ADT operations.
•In above example, the data structure
consists of only one element: a signed
sequence of digits. The corresponding
attribute is an ordinary integer of a
programming language1. We only define
two methods setValue() and addValue()
representing the two operations set and
add.
•Definition
•A class is the implementation of an
abstract data type (ADT).
•It defines attributes and methods which
implement the data structure and
operations of the ADT, respectively.
•Instances of classes are called objects.
Consequently, classes define properties
and behaviour of sets of objects
Example

• Write a C++ program that would instantiate a class


named integer with the following properties
a. A data member named a.
b. A member function named inputfor setting the
value of a 10.
c. A member function named display for outputting
the value of a.
1. include <iostream>
2. #include<string>
3. using namespace std;
4. class integer
5. {
6. public:
7. int input(int a)
8. {
9. a=10;// defined public data member
10.}
11. int display(int a)
12. {
13. cout<<"a ="<<a<<endl;
14. return(10);
15. }
16. };
17. int main()
18. {
19. integer set;
20. set.display(10);
21. return 0;
22.}
Assignment.
•Consider member functions that can be used to
achieve the following.
a)Get the employee details i.e
Name , Idno, Age,basic salary and total
amount_earned
b)A member function that can be used to display
employee details.
•Design a C++ object that can be used achieve
the above tasks.

You might also like