Jump to content


Photo

Help with C++ classes


  • Please log in to reply
5 replies to this topic

#1 AbalahDoon

AbalahDoon

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 29 June 2005 - 06:35 AM

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.

#2 soyunlocofl

soyunlocofl

    Young Padawan

  • Members
  • Pip
  • 70 posts
  • Location:FL

Posted 29 June 2005 - 01:20 PM

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.

Example
#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 AbalahDoon

AbalahDoon

    Young Padawan

  • Members
  • Pip
  • 3 posts

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 badrepent

badrepent

    Young Padawan

  • Members
  • Pip
  • 71 posts
  • Gender:Male
  • Location:Italy
  • Interests:WebDesign , CSS, XHTML, OOP, C#

Posted 30 June 2005 - 03:41 AM

Class MyClass

Simple example but remember that the keyword for declaring a class is
class


#5 AbalahDoon

AbalahDoon

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 30 June 2005 - 05:38 AM

yeah i copyed and pasted but the debugger caught that one.

#6 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

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):

#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