Classes are often constructed with two files, and used with a third
1) Header (.h)
Code file that contains the class template
2) Implementation (.cpp)
Code file that contains the class implementation
Defines the actions of the methods, etc…
3) Driver (.cpp)
Code file that uses these two files to create an instance of the object
___________________________________________________________________
// 1) Header (.h)
//Filename:name.h
class name
{
private:
char *_firstName;
char *_lastName;
public:
char *getFirstName();
char *getLastName();
void setFirstName(char*);
void setLastName(char*);
};
____________________________________________________________________________________
// 2) Implementation (.cpp)
//Filename: name.cpp
#include "name.h"
void name :: setFirstName(char *firstName)
{
_firstName = firstName;
}
void name :: setLastName(char *lastName)
{
_lastName = lastName;
}
char* name :: getFirstName()
{
return _firstName;
}
char* name :: getLastName()
{
return _lastName;
}
___________________________________________________________________________________
// 3) Driver (.cpp)
//Filename: NameDriver.cpp
#include <iostream>
#include "name.cpp"
using namespace std;
int main()
{
name myName;
myName.setFirstName("Honey");
myName.setLastName("singh");
cout << "Hello "
<< myName.getFirstName() << " "
<< myName.getLastName() << endl;
return 0;
}
___________________________________________________________________
OUTPUT:
Hello Honey singh
1) Header (.h)
Code file that contains the class template
2) Implementation (.cpp)
Code file that contains the class implementation
Defines the actions of the methods, etc…
3) Driver (.cpp)
Code file that uses these two files to create an instance of the object
___________________________________________________________________
// 1) Header (.h)
//Filename:name.h
class name
{
private:
char *_firstName;
char *_lastName;
public:
char *getFirstName();
char *getLastName();
void setFirstName(char*);
void setLastName(char*);
};
____________________________________________________________________________________
// 2) Implementation (.cpp)
//Filename: name.cpp
#include "name.h"
void name :: setFirstName(char *firstName)
{
_firstName = firstName;
}
void name :: setLastName(char *lastName)
{
_lastName = lastName;
}
char* name :: getFirstName()
{
return _firstName;
}
char* name :: getLastName()
{
return _lastName;
}
___________________________________________________________________________________
// 3) Driver (.cpp)
//Filename: NameDriver.cpp
#include <iostream>
#include "name.cpp"
using namespace std;
int main()
{
name myName;
myName.setFirstName("Honey");
myName.setLastName("singh");
cout << "Hello "
<< myName.getFirstName() << " "
<< myName.getLastName() << endl;
return 0;
}
___________________________________________________________________
OUTPUT:
Hello Honey singh
No comments:
Post a Comment