Classes Question.

This is a discussion on Classes Question. within the C++ Programming forums, part of the General Programming Boards category; So, im ok understanding the classes from this tutorial - Classes in C++ - Cprogramming.com , but what i can't ...

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    5

    Smile Classes Question.

    So, im ok understanding the classes from this tutorial -Classes in C++ - Cprogramming.com, but what i can't understand and im so curious about it, how to use Strings in classes? For example:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    
    
    class Computer
    {
    public:
    	Computer();
    	//Constructor
    
    
    	~Computer();
    	//Destructor
    
    
    	void setSpeed (int p);
    	int readSpeed();
    
    
    	void setMemorySpeed(int r);
    	int readMemorySpeed();
    protected:
    	int normalSpeed;
    	int memorySpeed;
    };
    
    
    Computer::Computer()
    {
    	normalSpeed = 0;
    	memorySpeed = 0;
    }
    Computer::~Computer()
    {
    
    
    
    
    }
    
    
    void Computer::setSpeed(int p)
    {
    	normalSpeed = p;
    }
    int Computer::readSpeed()
    {
    	return normalSpeed;
    }
    
    
    void Computer::setMemorySpeed(int r)
    {
    	memorySpeed = r;
    }
    int Computer::readMemorySpeed()
    {
    	return memorySpeed;
    }
    
    
    
    
    int main()
    {
    	Computer compute;
    
    
    	compute.setSpeed(150);
    	compute.setMemorySpeed(512);
    
    
    	cout<< "Your normal speed is: " << compute.readSpeed() << "\n"
    		<< "Your memory speed is: " << compute.readMemorySpeed() << "\n";
    	cin.get();
    }
    Here is the output: Name:  class.JPG
Views: 221
Size:  12.0 KB.

    Now, what i want is: a String that shows for example: Your Computer OS type is: Windows XP - SP3.

    Thank you infront.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Here is a trivial example.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
     
    class myString{
    private:
        string str;
    public:
        string example(){ return "Example string"; }
    };
    int main() 
    {
        myString eg;
        cout << eg.example() << endl;
        return 0;
    }
    I suggest you take a look at the std::string class.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    Quote Originally Posted by std10093 View Post
    Here is a trivial example.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
     
    class myString{
    private:
        string str;
    public:
        string example(){ return "Example string"; }
    };
    int main() 
    {
        myString eg;
        cout << eg.example() << endl;
        return 0;
    }
    I suggest you take a look at the std::string class.
    But what if i want to make another object of the same class to be with different returns? Like object myString eg is gonna return "Example string", now myString eg1 for example is going to return the same quotes , how can you change the arguments for each object created? Like the changing of the integers above in my code. Ty

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by Xam Enyap View Post
    how can you change the arguments for each object created? Like the changing of the integers above in my code. Ty
    same as integer - pass it to constructor of the class or create some setter member function which will accept string and store it in the member variable
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    5
    Quote Originally Posted by vart View Post
    same as integer - pass it to constructor of the class or create some setter member function which will accept string and store it in the member variable
    How am i supposed to create a Setter member function which accept string? Thats im wondering all the time

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Code:
    private:
       std::string m_exsMember;
    public:
        void example(const std::string& val){ m_exsMember=val; }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about classes
    By naroken in forum C++ Programming
    Replies: 3
    Last Post: 04-29-2009, 10:37 AM
  2. A little question about classes
    By Newbeee in forum C++ Programming
    Replies: 9
    Last Post: 06-15-2006, 03:46 PM
  3. Question about Classes
    By WildFire in forum C++ Programming
    Replies: 8
    Last Post: 06-18-2004, 07:57 PM
  4. Question on use of classes
    By Flyer in forum C++ Programming
    Replies: 8
    Last Post: 06-25-2003, 08:23 AM
  5. Another question on classes
    By hpy_gilmore8 in forum C++ Programming
    Replies: 26
    Last Post: 05-24-2003, 09:11 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21