hey 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.
Help with C++ classes
Started by AbalahDoon, Jun 29 2005 06:35 AM
5 replies to this topic
#1
Posted 29 June 2005 - 06:35 AM
#2
Posted 29 June 2005 - 01:20 PM
Quote
hey 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.
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
Hey thanks very much soyunlocofl. i didnt know about the string class. thats very conveinient. do you happen to know a way to do it without using the string class? im just not sure how to pass the arguments to the member functions to set the name. thanks again.
#4
Posted 30 June 2005 - 03:41 AM
soyunlocofl, on Jun 29 2005, 06:20 PM, said:
Class MyClass
class
#5
Posted 30 June 2005 - 05:38 AM
yeah i copyed and pasted but the debugger caught that one.
#6
Posted 17 July 2005 - 07:22 AM
Not sure if this is what you were looking for, but I prefer this way ('Cause it's much easier):
Quote
#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");
}
#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");
}
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
