My First programming made with Classes and such useless but it works

This is a discussion on My First programming made with Classes and such useless but it works within the C++ Programming forums, part of the General Programming Boards category; and it is conventional to capitalize the first letter of class names, eg person instead of Person and to give ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    and it is conventional to capitalize the first letter of class names, eg

    person instead of Person

    and to give variables lowercase names, eg

    password instead of PassWord (for multi-word names, there are two conventions - first_second and firstSecond, pick one and stick with it)

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Is it just me or is there something funny going on with dates?...

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Since a class Person is supposed to represent "a" individual, you should construct 3 objects and give them identities. A class is supposed to be "a" object.
    And yes, the thread is entirely messed up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by cyberfish View Post
    Is it just me or is there something funny going on with dates?...
    Looks like the server is messing around with timezones, posting each message in sequence with the time of the client submitting it without taking into account the timezone it was posted from.

  5. #5
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Cool My First programming made with Classes and such useless but it works

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    void ErrorMessage();
    
    class person {
    
    
    	public:
    
    		person (int InitialAge) { JoshAge = InitialAge, SteveAge = InitialAge, KaylaAge = InitialAge; }
    
    		~person();
    
    		int GetJoshAge() { return JoshAge; }
    		int GetSteveAge() { return SteveAge; }
    		int GetKayleAge() { return KaylaAge; }
    		void SetAge(int age) { JoshAge = age, SteveAge = age, KaylaAge = age; }
    		void HellO_S() { cout << "HI Steve Welcome back\n"; }
    		void HellO_K() { cout << "HI Kayla Welcome Back \n"; }
    		void HellO_J() { cout << "HI Josh Welcome Back \n"; }
    
    
    
    	private:
    
    		int JoshAge;
    		int SteveAge;
    		int KaylaAge;
    
    };
    
    person::~person() {
    }
    
    int main() {
    
    
    	int PassWord;
    	enum AllName { Steve, Kayla, Josh, Names };
    	int ArrName[Names] = {1,2,3};
    
    	
    
    	cout << "Enter your Password \n>";
    	cin >> PassWord;
    
    	cout << endl;
    
    	person everyone(17);
    
    	if ( PassWord != 1234) {
    		
    		ErrorMessage();
    
    	} else if ( PassWord == 1234 ) {
    
    		cout << ArrName[Steve] << " = Steve  \n\n";
    		cout <<  ArrName[Kayla] << " = Kayla  \n\n";
    		cout << ArrName[Josh] << " = Josh \n\n";
    
    		cout << "Enter your name number to gain excess to your files \n>";
    		cin >> ArrName[Names];
    
    		if ( ArrName[Names] == 1 ) {
    
    			everyone.HellO_S();
    
    			cout << "Your age is " << everyone.GetSteveAge() << "\n";
    
    		} else if ( ArrName[Names] == 2 ) {
    
    			everyone.SetAge(14);
    
    			everyone.HellO_K();
    
    			cout << "Your age is " << everyone.GetKayleAge() << "\n";
    
    		} else if ( ArrName[Names] == 3 ) {
    
    			everyone.SetAge(12);
    
    			everyone.HellO_J();
    
    			cout << "Your age is " << everyone.GetJoshAge() << "\n";
    
    		} else {
    
    			ErrorMessage();
    
    		}
    		
    
    	}
    
    
    
    	system("pause");
    
    
    	return(0);
    
    
    }
    
    void ErrorMessage() {
    
    	cout << "ERROR!!!!\n";
    	cout << "This program with self close in 2 sec\n";
    
    	Sleep(2000);
    	exit(1);
    }
    Yes Yes Very useless but I don't care Tell me what i should work on and error you see, it compile find doesn't mean there's not errors.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,167
    I think 'Person' should represent "Person" and not "Three Persons".

    Soma

  7. #7
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    Code:
    #include <iostream>
    #include <string>
    #include <stdio.h>
    
    
    
    using namespace std;
    
    class Employee {
    
    	public:
    
    		int GetAge() { return hisAge; }
    		int GetYears() { return hisYears; }
    		int GetSalary() { return hisSalary; }
    
    		void SetAge(int age) { hisAge = age; }
    		void SetYears(int years) { hisYears = years; }
    		void SetSalary(int salary) { hisSalary = salary; }
    
    		void Greeting() { cout << "Hell'O, Welcome Back\n"; }
    
    	private:
    
    		int hisAge;
    		int hisYears;
    		int hisSalary;
    
    };
    
    
    
    int main() {
    
    
    	Employee Steve;
    
    	Steve.SetAge(17);
    	Steve.SetSalary(20000);
    	Steve.SetYears(2);
    
    	cout << "Employee name is Steve. He is " << Steve.GetAge() << " and been working for us for about " << Steve.GetYears() << "\n";
    	cout << "Steve makes about " << Steve.GetSalary() << " a year \n";
    
    
    	system("pause");
    
    	return(0);
    
    
    }
    How about this one better or not?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by Sshakey6791
    How about this one better or not?
    It is better, though you might want to provide a constructor so that you can initialise the Employee object with the given data, and perhaps you should also be able to get the employee's name given the corresponding Employee object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, it's better. But perhaps the part that connects the name "Steve" to the particular object should also be part of the object (so instead of doing
    Code:
    Employee Steve(17, 20000, 2);
    , you do
    Code:
    Employee emp("Steve", 17, 20000, 2);
    )
    That way, you don't have to write in your code itself the name of the employee (or at least, only in ONE place).

    Edit: Too slow, Laserlight beat me to it...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57
    I'm really bad at constructor i'm really not 100&#37; sure how to do them can you give me an example?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Example:
    Code:
    class Employee
    {
    public:
        Employee(std::string Name, int Age, int Salary, int Years): m_Name(Name), m_Age(Age), m_Salary(Salary), m_Years(Years) {}
        Employee() {}
    
    private:
        std::string m_Name;
        int m_Years;
        int m_Salary;
        int m_Age;
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

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