-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70dc493
commit 6df7e19
Showing
551 changed files
with
23,411 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Demonstrates a simple namespace | ||
#include <iostream> | ||
using namespace std; | ||
|
||
namespace test | ||
{ | ||
int x, y, z; | ||
} | ||
|
||
int main() | ||
{ | ||
test::x = 10; | ||
test::y = 20; | ||
test::z = 30; | ||
cout << "The values are:\n"; | ||
cout << test::x << " " << test::y | ||
<< " " << test::z << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Demonstrates two namespaces | ||
#include <iostream> | ||
using namespace std; | ||
|
||
namespace test1 | ||
{ | ||
int x, y, z; | ||
} | ||
|
||
namespace test2 | ||
{ | ||
int x, y, z; | ||
} | ||
|
||
int main() | ||
{ | ||
test1::x = 10; | ||
test1::y = 20; | ||
test1::z = 30; | ||
cout << "The test1 values are:\n"; | ||
cout << test1::x << " " << test1::y | ||
<< " " << test1::z << endl; | ||
test2::x = 1; | ||
test2::y = 2; | ||
test2::z = 3; | ||
cout << "The test2 values are:\n"; | ||
cout << test2::x << " " << test2::y | ||
<< " " << test2::z << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// A demonstration of the using namespace statement. | ||
#include <iostream> | ||
#include "nsdemo.h" | ||
using namespace std; | ||
using namespace demo; | ||
|
||
int main() | ||
{ | ||
testObject.x = 10; | ||
testObject.y = 20; | ||
testObject.z = 30; | ||
cout << "The values are:\n" | ||
<< testObject.x << " " | ||
<< testObject.y << " " | ||
<< testObject.z << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// This file defines a namespace named demo. | ||
// In the demo namespace a class named NsDemo | ||
// is declared, and an instance of the class | ||
// named testObject is defined. | ||
|
||
namespace demo | ||
{ | ||
class NsDemo | ||
{ | ||
public: | ||
int x, y, z; | ||
}; | ||
|
||
NsDemo testObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// This program demonstrates how to read | ||
// command line arguments. | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
cout << "You entered " << (argc - 1); | ||
cout << " command line arguments.\n"; | ||
if (argc > 1) | ||
{ | ||
cout << "Here they are:\n"; | ||
for (int count = 1; count < argc; count++) | ||
cout << argv[count] << endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// This program takes two command line arguments, | ||
// assumed to be numbers, and displays their sum. | ||
#include <iostream> | ||
#include <cmath> // Needed for atof | ||
using namespace std; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
double total = 0; | ||
|
||
if (argc > 1) | ||
{ | ||
for (int count = 1; count < argc; count++) | ||
total += atof(argv[count]); | ||
cout << total << endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Pet class implementation file | ||
#include <string> | ||
#include "Pet.h" | ||
using namespace std; | ||
|
||
// Constructor | ||
Pet::Pet(string str) | ||
{ | ||
name = str; | ||
} | ||
|
||
// getName member function | ||
string Pet::getName() const | ||
{ | ||
return name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Pet class specification file | ||
#ifndef PET_H | ||
#define PET_H | ||
#include <string> | ||
using namespace std; | ||
|
||
class Pet | ||
{ | ||
private: | ||
string name; // To hold the pet's name | ||
|
||
public: | ||
// Constructor | ||
Pet(string); | ||
|
||
// Accessor function for name | ||
string getName() const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <iostream> | ||
#include "Pet.h" | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
// Create an instance of the Pet class. | ||
Pet myPet("Fido"); | ||
|
||
// Display my pet's name. | ||
cout << "My pet's name is " << myPet.getName() << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// This program uses the Rectangle class, which is declared in | ||
// the Rectangle.h file. The Rectangle class's member functions | ||
// are defined in the Rectangle.cpp file. This program should | ||
// be compiled with those files in a project. | ||
#include <iostream> | ||
#include "Rectangle.h" // Needed for Rectangle class | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
Rectangle box; // Define an instance of the Rectangle class | ||
double rectWidth; // Local variable for width | ||
double rectLength; // Local variable for length | ||
|
||
// Get the rectangle's width and length from the user. | ||
cout << "This program will calculate the area of a\n"; | ||
cout << "rectangle. What is the width? "; | ||
cin >> rectWidth; | ||
cout << "What is the length? "; | ||
cin >> rectLength; | ||
|
||
// Store the width and length of the rectangle | ||
// in the box object. | ||
box.setWidth(rectWidth); | ||
box.setLength(rectLength); | ||
|
||
// Display the rectangle's data. | ||
cout << "Here is the rectangle's data:\n"; | ||
cout << "Width: " << box.getWidth() << endl; | ||
cout << "Length: " << box.getLength() << endl; | ||
cout << "Area: " << box.getArea() << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Implementation file for the Rectangle class. | ||
#include "Rectangle.h" // Needed for the Rectangle class | ||
#include <iostream> // Needed for cout | ||
#include <cstdlib> // Needed for the exit function | ||
using namespace std; | ||
|
||
//*********************************************************** | ||
// setWidth sets the value of the member variable width. * | ||
//*********************************************************** | ||
|
||
void Rectangle::setWidth(double w) | ||
{ | ||
if (w >= 0) | ||
width = w; | ||
else | ||
{ | ||
cout << "Invalid width\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
//*********************************************************** | ||
// setLength sets the value of the member variable length. * | ||
//*********************************************************** | ||
|
||
void Rectangle::setLength(double len) | ||
{ | ||
if (len >= 0) | ||
length = len; | ||
else | ||
{ | ||
cout << "Invalid length\n"; | ||
exit(EXIT_FAILURE); | ||
} | ||
} | ||
|
||
//*********************************************************** | ||
// getWidth returns the value in the member variable width. * | ||
//*********************************************************** | ||
|
||
double Rectangle::getWidth() const | ||
{ | ||
return width; | ||
} | ||
|
||
//************************************************************* | ||
// getLength returns the value in the member variable length. * | ||
//************************************************************* | ||
|
||
double Rectangle::getLength() const | ||
{ | ||
return length; | ||
} | ||
|
||
//************************************************************ | ||
// getArea returns the product of width times length. * | ||
//************************************************************ | ||
|
||
double Rectangle::getArea() const | ||
{ | ||
return width * length; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Specification file for the Rectangle class. | ||
#ifndef RECTANGLE_H | ||
#define RECTANGLE_H | ||
|
||
// Rectangle class declaration. | ||
|
||
class Rectangle | ||
{ | ||
private: | ||
double width; | ||
double length; | ||
public: | ||
void setWidth(double); | ||
void setLength(double); | ||
double getWidth() const; | ||
double getLength() const; | ||
double getArea() const; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// This program uses the Rectangle class's specification | ||
// and implementation files. | ||
#include <iostream> | ||
#include "Rectangle.h" // Contains the class declaration. | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
Rectangle box; // Define an instance of the class. | ||
float rectWidth, // Local varibale for width. | ||
rectLength; // Local variable for length. | ||
|
||
// Get the rectangle's width and length from the user. | ||
cout << "This program will calculate the area of a\n"; | ||
cout << "rectangle. What is the width? "; | ||
cin >> rectWidth; | ||
cout << "What is the length? "; | ||
cin >> rectLength; | ||
|
||
if (!box.setWidth(rectWidth)) // Store the width. | ||
cout << "Invalid value for width.\n"; | ||
else if (!box.setLength(rectLength)) // Store the length. | ||
cout << "Invalid value for length.\n"; | ||
else | ||
{ | ||
// Display the rectangle's data. | ||
cout << "Here is the rectangle's data:\n"; | ||
cout << "Width: " << box.getWidth() << endl; | ||
cout << "Length: " << box.getLength() << endl; | ||
cout << "Area: " << box.getArea() << endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// This program asks for sales figures for 3 days. The total | ||
// sales is calculated and displayed in a table. | ||
#include <iostream> | ||
#include <iomanip> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
double day1, day2, day3, total; | ||
|
||
cout << "Enter the sales for day 1: "; | ||
cin >> day1; | ||
cout << "Enter the sales for day 2: "; | ||
cin >> day2; | ||
cout << "Enter the sales for day 3: "; | ||
cin >> day3; | ||
|
||
total = day1 + day2 + day3; | ||
|
||
cout.precision(2); | ||
cout.setf(ios::fixed | ios::showpoint); | ||
|
||
cout << "\nSales Figures\n"; | ||
cout << "-------------\n"; | ||
cout << "Day 1: " << setw(8) << day1 << endl; | ||
cout << "Day 2: " << setw(8) << day2 << endl; | ||
cout << "Day 3: " << setw(8) << day3 << endl; | ||
cout << "Total: " << setw(8) << total << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This program uses cin's width member function. | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
char word[5]; | ||
|
||
cout << "Enter a word: "; | ||
cin.width(5); | ||
cin >> word; | ||
cout << "You entered " << word << endl; | ||
return 0; | ||
} |
Oops, something went wrong.