Syntax
Syntax
Lecture 3
Classes :
Class Syntax Class Example
Class ClassName { class Rectangle {
declaration; double width; | Data (Attributes) :
declaration; double length; | define state in objects
};
displayWidth( ); | Functions : define the
displayLength( ); | behavior of objects
displayArea( ); |
};
Object :
Object Syntax Object Example
ClassName objectName; Rectangle r;
Access Specifiers:
Access Specifiers Syntax Access Specifiers Example
class Rectangle { class Rectangle {
private: private:
declaration; double width;
declaration; double length;
public: public:
declaration; getWidth( ) { return width; }
}; };
Member Functions:
in-line member function out-of-line member functions
Example Syntax
class Rectangle { returnType ClassName::MemberFunction( )
private: {
double width; …………
double length; }
public: Example
double calcArea( ) class Rectangle {
{ private:
return width * length; //inline double width;
} double length;
}; public:
double calcArea( ) //prototype
}; //class declaration ends
double Rectangle::calcArea( )
{
return width * length; //out-of-line
}
Const Member Functions :
Syntax Example
in-line member function class Rectangle {
returnType MemberFunction const ( ) private:
{ double width;
………… double length;
} public:
out-of-line member functions void setWidth(double);
returnType ClassName::MemberFunction( ) void setLength(double);
{ double getWidth( ) const { return width; }
………… double getLength( ) const { return length; }
} double getArea( ) const { return width * length; }
};
Pointer to an Object:
Can define a pointer to an object:
Rectangle *rPtr;
rPtr = &otherRectangle;
Can access public members via pointer. You can use * and . OR - >
rPtr - >setLength(12.5);
rPtr - >setWidth(4.8);
cout << rPtr - >getLenght( ) << endl;
Reference to Objects:
Reference and Pointers to Objects :
Code Output
class Rectangle { Area (object) = 20
private: Area (pointer) = 20
int w; Area (reference to object) =
int h; 20
public: Area (ref to pointer) = 20
void setWidth(int ww) { w = ww; } ______________________
void setHeight(int hh) { h = hh; }
double getArea( ) const { return w * h; }
};
int main( )
{
Rectangle r1;
Rectangle *ptr = &r1;
Rectangle &ref = r1;
Rectangle* &ref2 = ptr;
r1.setWidth( 5 );
r1.setHeight( 4 );
cout<<“\n Area (object) = “<<r1.getArea( );
cout<<“\n Area (pointer) = “<<ptr->getArea( );
cout<<“\n Area (reference to object) = “<<ref.getArea( );
cout<<“\n Area (ref to pointer) = “<<ref->getArea( );
return 0;
}
Separating Specification from Implementation:
Time Class with Time1.h Time Class with Time1.cpp
// time1.h - Declaration of the Time Class
// Member function definition for Time Class
// Member functions are defined in time1.cpp
3. #include <iostream>
// prevent multiple inclusions of header file using std::cout
#ifndef TIME1_H Dot(.) replaced with #include “time1.h” Source file uses #include to
#define TIME1_H underscore(_) in file name. load the header file
// Constructor initializes each data member to
// Time abstract data type definition zero. Ensures all Time objects start in a
class Time { consistent state.
public:
Time( ); Time::Time( )
//constructor {
void setTime(int,int,int); //set hour = minute = second = 0;
hour }
void printMiliatry( ); //print // Set time value. Perform validity checks
void printStandard( ); //print
private: void Time::setTime( int h, int m, int s )
int hour; // 0 -23 {
int minute; // 0 - 59 hour = ( h >= 0 && h < 24 ) ? h : 0;
int second; // 0 - 59 minute = ( m >= 0 && h < 60 ) ? m : 0;
}; second = ( s >= 0 && s < 60 ) ? s : 0; 35.
}
#endif
// Print time in military formal
void Time::printMilitary( )
{
cout << ( hour < 10 ? “0” : “ ”) << hour << “ :
”
<< ( minute < 10 ? “0” : “ ”) << minute ;
}
// Print time in military formal
void Time::printStandard( )
{
cout << ((hour == 0 || hour == 12) ? 12 : hour
% 12 )<< “ : ” << ( minute < 10 ? “0” : “ ”) <<
minute << ( second < 10 ? “0” : “ ”) <<
second
<< ( hour < 12 ? “AM” : “PM”) ;
}
Constructors :
Constructor (in-line member function) Constructor (out-of-line member functions)
class Demo { class Demo {
public: public:
Demo( ) Demo( ); //constructor
{ };
//inline function - constructor
cout<<“Welcome to the constructor”; Demo::Demo( )
} {
}; //out of line function - constructor
cout<<“Welcome to the constructor”;
}