Help with C++ classes
#1
Posted 29 June 2005 - 06:35 AM
i cant seem to get the char string right. i would like it so that i could write say
myClass.SetName() sort of thing but i have no idea how i should set up the string to start with and what parameters i need to pass etc.
what i would really appreciate is if someone could spend a couple of minutes writing some code for a class that has 3 vars, name, age and height say. and then show how to use the class. I think learning by example is the best way so here i am hoping to find someone who can help me.
Well thanks very much
Danny.
#2
Posted 29 June 2005 - 01:20 PM
Examplehey ive just started learning C++. i would like to to create a class that holds a char string (for name), and just a couple of other integers.
i cant seem to get the char string right. i would like it so that i could write say
myClass.SetName() sort of thing but i have no idea how i should set up the string to start with and what parameters i need to pass etc.
what i would really appreciate is if someone could spend a couple of minutes writing some code for a class that has 3 vars, name, age and height say. and then show how to use the class. I think learning by example is the best way so here i am hoping to find someone who can help me.
Well thanks very much
Danny.
#include <iostream> #include <string> using namespace std; Class MyClass { public: MyClass(); // Constructor ~MyClass(); //Destructor void SetName(); void PrintAll(); void SetAge(); void SetHeight; private: string name; int age; string height; }; MyClass::Student() { name = ""; age = 0; height = 0; } MyClass::~Student() { // Destructor } void MyClass::SetName() { cout << " Enter your first name: " << endl; cin >> name; cout << endl; } void MyClass::SetAge() { cout << " Enter your Age: " << endl; cin >> age; cout << endl; } void MyClass::SetHeight() { cout << " Enter your Height: " << endl; cin >> height; cout << endl; } void MyClass::PrintAll() { cout << " Name: " << name << endl; cout << " Age: " << age << endl; cout << " Height: " << height << endl; } int main() { MyClass example; example.SetName(); example.SetAge(); example.SetHeight(); example.PrintAll(); return 0; }
#3
Posted 30 June 2005 - 12:32 AM
#4
Posted 30 June 2005 - 03:41 AM
Simple example but remember that the keyword for declaring a class isClass MyClass
class
#5
Posted 30 June 2005 - 05:38 AM
#6
Posted 17 July 2005 - 07:22 AM
#include <iostream>
#include <string>
using namespace std;
/*
If you find this source code useful
and want to use it in any of your
projects, please contact me:
dj_indigo88<at>hotmail.com
subject: C++
*/
int main()
{
string name = "";
int age;
string height = "";
cout << "Please enter your name:\n";
cin >> name;
cout << "Please enter your age:\n";
cin >> age;
cout << "Please enter your height:\n";
cin >> height;
cout << "----------------------------------------\n";
cout << "Your name: " << name << endl;
cout << "Your age: " << age << endl;
cout << "Your height: " << height << endl;
cout << "----------------------------------------\n";
system("pause");
}
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users