Anda di halaman 1dari 20

SFG

Sal F. Gambino

Review
of
Class
members
1
Class- unit of programming from SFG
which objects are eventually created Sal F. Gambino

• A class consists of
member functions
and data members.
• A data member is
a data component
of a class.
• A member function
is a function
component of a
class.
2
Member Access Specifiers:
SFG
Public Sal F. Gambino

• One member access


specifier is public..
• In public, any data
member declared after
public and before the
next member access
specifier is accessible
wherever the program
has access to an object
of the class. 3
Member Access Specifier: SFG
Private Sal F. Gambino

• Another member
access specifier is
private.
• In private, any data
member declared after
private is accessible
only to member
functions of the class.

4
SFG
Constructor Sal F. Gambino

• A constructor is a
class member
function with the
same name as the
class. It initializes the
object that is created.

5
SFG
An example of a class
Sal F. Gambino
• Assume that we have a • class ID
class called ID. { public:
• Notice how the ID( );
constructor ( ID( ); ) is the void personal( );
same name as the class. private:
• Notice the left and right char firstname;
braces. int age;
• Notice the member access char lastname;
specifiers public & char gender;
private };
6
Member Functions & SFG
Data Members Usually theSal F. Gambino
member
functions
• Class ID are listed under
{ public: ID( ); the public specifier
void personal( );
private: char firstname;
int age;
char lastname;
char gender; The data members
variables used
};
in the class
7
SFG
Here is how a member function
looks in the actual program Sal F. Gambino

void ID::personal( )
{ cout<<“Enter in your first name”;
cin>> firstname;
cout<<“Enter in your last name”;
cin>> lastname;
cout<<“Enter in your age (number form)”;
cin>> age;
cout<<“Enter in your gender (M/F)”;
cin>> gender;
}
8
SFG
Oops…Almost forgot about the
binary scope operator! Sal F. Gambino

Binary scope
selection operator-
::personal( )
ID
connects member {
function with class
name.
}
Here is the binary
scope selection
operator.
9
SFG
Here’s how to declare Objects
of the class in MAIN Sal F. Gambino

• The program #include <iostream.h>


starts by #include <stdlib.h>
activating the #include ”id.h"
personal
int main( )
member {
function for ID student;
student. student.personal();

10
SFG
Here’s how to declare Objects of
the class in MAIN Sal F. Gambino

The member
function
int main( )
activation {
consists of four ID student;
parts, starting
with the object student.personal();
name.

11
SFG
Here’s how to declare Objects of
the class in MAIN Sal F. Gambino

The instance
name is int main( )
followed by a {
ID student;
period.

.
student personal( );

12
Here’s how to declare Objects of
SFG
the class in MAIN Sal F. Gambino

After the
period is the
name of the
member
function that
you are int main( ) {
activating. ID student;

student.personal();

13
Here’s how to declare Objects of
SFG
the class in MAIN Sal F. Gambino

Finally, the #include ”id.h"


arguments for
the member int main( ) {
function. In class c;
this example
c.personal( );
there are no
arguments

14
SFG
Here’s how to declare Objects
of the class in MAIN Sal F. Gambino

• The program #include <iostream.h>


starts by #include <stdlib.h>
activating the #include ”rectangle.h"
area
int main( )
member {
function for rectangle rect1;
rectangle. rect1.area ( 4, 5 );

15
SFG
Here’s how to declare Objects of
the class in MAIN Sal F. Gambino

The member
function
int main( )
activation {
consists of four rectangle rect1;
parts, starting
with the object rect1.area ( 4, 5 );
name.

16
SFG
Here’s how to declare Objects of
the class in MAIN Sal F. Gambino

The instance
name is int main( )
followed by a {
rectangle rect1;
period.

.
rect1 area ( 4, 5 );

17
Here’s how to declare Objects of
SFG
the class in MAIN Sal F. Gambino

After the
period is the
name of the
member
function that
you are int main( ) {
activating. rectangle rect1;

rect1.area ( 4, 5 );

18
Here’s how to declare Objects of
SFG
the class in MAIN Sal F. Gambino

Finally, the
#include ”rectangle.h"
arguments for
the member int main( ) {
function. In rectangle rect;
this example
there are two rect1.area ( 4, 5 );
arguments, 4
and 5

19
SFG
Summary Sal F. Gambino

• Learned about all objects associated with a


class.
• Learned what each object does
• Created a Class
– how to insert one
– where each part is inserted
– why it is inserted

20