Demo - Class Person
Two files per class
Programmers typically put all code for a class into two files, separate from other code.
ClassName.h contains the class definition, including data members and member function declarations.
ClassName.cpp contains member function definitions.
Person.cpp
// Person.cpp
// Created by Kevin Roark on 11/13/22.
// This is a C++ source file that defines a class called "Person".
// The class has member functions to manipulate a person's first, middle, and last name.
// Include necessary header files
#include <iostream> // Input/output operations
#include <string> // String operations
#include "Person.hpp" // User-defined header file for the Person class
// Use the "std" namespace to avoid writing "std::" before each standard library function
using namespace std;
// Define the member function "print" that prints the first, middle, and last name of a person
void Person::print() const
{
cout << firstName << " " << middleName << " " << lastName;
}
// Define the member function "setName" that sets the first, middle, and last name of a person
void Person::setName(string first, string middle, string last)
{
firstName = first;
middleName = middle;
lastName = last;
}
// Define the member function "setLastName" that sets the last name of a person
void Person::setLastName(string last)
{
lastName = last;
}
// Define the member function "setFirstName" that sets the first name of a person
void Person::setFirstName(string first)
{
firstName = first;
}
// Define the member function "setMiddleName" that sets the middle name of a person
void Person::setMiddleName(string middle)
{
middleName = middle;
}
// Define the member function "getFirstName" that returns the first name of a person
string Person::getFirstName() const
{
return firstName;
}
// Define the member function "getMiddleName" that returns the middle name of a person
string Person::getMiddleName() const
{
return middleName;
}
// Define the member function "getLastName" that returns the last name of a person
string Person::getLastName() const
{
return lastName;
}
// Define the constructor function that initializes the first, middle, and last name of a person using provided parameters
Person::Person(string first, string middle, string last)
{
firstName = first;
middleName = middle;
lastName = last;
}
This C++ code defines a class called "Person" in a source file named "Person.cpp". The purpose of this class is to represent a person's first, middle, and last name, and it provides member functions to manipulate and access these names.
Here's a breakdown of the code:
The code includes necessary header files:
#include <iostream>
: This header file provides input/output operations, such ascout
for outputting data to the console.#include <string>
: This header file provides string operations and functions.#include "Person.hpp"
: This includes a user-defined header file "Person.hpp", which likely contains the class declaration for the Person class.
The code uses the
std
namespace to avoid writingstd::
before each standard library function. This allows the code to directly use functions likecout
without thestd::
prefix.The code defines the member function
print()
that prints the first, middle, and last name of a person. The function usescout
to output the names to the console.The code defines the member function
setName()
that sets the first, middle, and last name of a person. The function takes three string parameters:first
,middle
, andlast
. It assigns these parameter values to the corresponding member variables of the Person class (firstName
,middleName
, andlastName
).The code defines the member function
setLastName()
that sets the last name of a person. It takes a string parameterlast
and assigns its value to thelastName
member variable.The code defines the member function
setFirstName()
that sets the first name of a person. It takes a string parameterfirst
and assigns its value to thefirstName
member variable.The code defines the member function
setMiddleName()
that sets the middle name of a person. It takes a string parametermiddle
and assigns its value to themiddleName
member variable.The code defines the member function
getFirstName()
that returns the first name of a person. It is a const member function and does not modify the object. It simply returns the value of thefirstName
member variable.The code defines the member function
getMiddleName()
that returns the middle name of a person. Similar togetFirstName()
, it is a const member function that returns the value of themiddleName
member variable.The code defines the member function
getLastName()
that returns the last name of a person. It is also a const member function that returns the value of thelastName
member variable.The code defines a constructor function for the Person class. The constructor takes three string parameters
first
,middle
, andlast
, and initializes the corresponding member variablesfirstName
,middleName
, andlastName
with the provided parameter values.
Overall, this code provides a basic implementation of a Person class with functions to manipulate and access the person's first, middle, and last name.
Person.hpp or Person.h
// Person.hpp
// Created by Kevin Roark on 11/13/22.
#ifndef Person_hpp
#define Person_hpp
#include <stdio.h>
#include <string>
using namespace std;
class Person
{
public:
//getters and setters declarations
void print() const;
void setName(string first, string middle, string last);
void setLastName(string last);
void setFirstName(string first);
void setMiddleName(string middle);
string getFirstName() const;
string getMiddleName() const;
string getLastName() const;
//constructor
Person(string first = "", string middle = "", string last = "");
private:
//class attributes / properties
string firstName;
string middleName;
string lastName;
};
#endif /* Person_hpp */
This C++ code is a header file named "Person.hpp" that accompanies the previously explained "Person.cpp" file. The header file contains the class declaration for the Person class, including its member functions and member variables.
Here's a breakdown of the code:
The code includes the necessary header files:
#include <stdio.h>
: This header file is included but not used in the code. It can be removed.#include <string>
: This header file is included to use the string data type and related functions.
The code uses the
std
namespace to avoid writingstd::
before each standard library function. This allows the code to directly use functions likestring
without thestd::
prefix.The class declaration begins with the
class Person
line, which introduces the Person class.The
public:
section defines the public interface of the class. These are the functions and member variables that can be accessed from outside the class.The code declares the member functions (methods) of the Person class. These functions include:
void print() const;
: This member function prints the first, middle, and last name of a person.void setName(string first, string middle, string last);
: This member function sets the first, middle, and last name of a person.void setLastName(string last);
: This member function sets the last name of a person.void setFirstName(string first);
: This member function sets the first name of a person.void setMiddleName(string middle);
: This member function sets the middle name of a person.string getFirstName() const;
: This member function returns the first name of a person.string getMiddleName() const;
: This member function returns the middle name of a person.string getLastName() const;
: This member function returns the last name of a person.
The
private:
section defines the private members of the class. These are the member variables that can only be accessed within the class itself.The code declares the member variables (attributes or properties) of the Person class. These variables include:
string firstName;
: This member variable holds the first name of a person.string middleName;
: This member variable holds the middle name of a person.string lastName;
: This member variable holds the last name of a person.
The code also declares a constructor for the Person class. The constructor takes three string parameters:
first
,middle
, andlast
, and provides default values of an empty string for all three parameters. The constructor is responsible for initializing the member variablesfirstName
,middleName
, andlastName
with the provided parameter values.
Overall, this header file defines the structure and interface of the Person class, including its member functions and member variables. It provides a blueprint for creating Person objects and performing operations on them. The corresponding implementation of the class can be found in the "Person.cpp" file.
main.cpp
// main.cpp
// Created by Kevin Roark on 11/3/22.
// Driver to demo the Person Class
#include <iostream>
#include "Person.hpp"
using namespace std;
int main()
{
Person student("Kevin", "Read", "Roark");
student.print();
cout << endl;
cout << student.getLastName();
student.setLastName("Flintstone");
cout << endl;
student.print();
cout << endl;
Person studentTwo("Sam", "R.", "Smith");
cout << endl;
studentTwo.print();
cout << endl;
return 0;
}
This C++ code is a main program file named "main.cpp". It serves as a driver program to demonstrate the usage of the Person class defined in the "Person.hpp" and "Person.cpp" files.
Here's a breakdown of the code:
The code includes the necessary header files:
#include <iostream>
: This header file provides input/output operations, such ascout
for outputting data to the console.#include "Person.hpp"
: This includes the user-defined header file "Person.hpp", which contains the class declaration for the Person class.
The code uses the
std
namespace to avoid writingstd::
before each standard library function. This allows the code to directly use functions likecout
without thestd::
prefix.The
main()
function is the entry point of the program. Execution of the program starts from here.In the
main()
function, an instance of the Person class namedstudent
is created using the constructor. The constructor is called with the arguments "Kevin", "Read", and "Roark". This initializes thestudent
object with the given first, middle, and last name.The
print()
member function of thestudent
object is called using the dot operator (.
) to print the first, middle, and last name of the student.The
getLastName()
member function of thestudent
object is called to retrieve and print the last name of the student.The
setLastName()
member function of thestudent
object is called to change the last name of the student to "Flintstone".The
print()
member function of thestudent
object is called again to print the updated first, middle, and last name.Another instance of the Person class named
studentTwo
is created using the constructor. The constructor is called with the arguments "Sam", "R.", and "Smith". This initializes thestudentTwo
object with the given first, middle, and last name.The
print()
member function of thestudentTwo
object is called to print the first, middle, and last name of the second student.The program ends by returning 0 from the
main()
function, indicating successful execution.
Overall, this main program demonstrates the usage of the Person class by creating objects, setting and retrieving their names, and printing the names to the console.
2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark