Intro to C++ [Classes, Nesting, Casting, Enums, Low Lever Error Checking and more...] menu

User Tag List

Results 1 to 1 of 1
  1. #1
    DarkLinux's Avatar Former Staff
    CoreCoins Purchaser Authenticator enabled
    Reputation
    1627
    Join Date
    May 2010
    Posts
    1,846
    Thanks G/R
    193/539
    Trade Feedback
    16 (100%)
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)

    Intro to C++ [Classes, Nesting, Casting, Enums, Low Lever Error Checking and more...]

    So this is a little example of some beginner C++ fundamentals. This will go over Classes, Nesting, Casting, Enums, Low Lever Error Checking, Sending Values to a functions and receiving Values from a functions. This is just a simple Console Application, there for no GUI. If anyone takes an interest I will explain the code in more detail if needed.

    This program was to replicate a business program at the same time cover the key OOP parts of C++. The code takes the users input for multiple things like date of higher and salary. A simple algorithm was used to determine if its a leap year.

    Over all its a good start into OOP and C++. More to come b/c I am helping a friend code out this stuff for his class. Please ask question or post requests!

    Code:
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    
    enum Category{manager, laborer, secretary};
    
    class Date
    {
    private:
    	int year, month, day;
    public:
    	void Set()
    	{
    		cout << "Enter In The Year : "; cin >> year;
    		cout << "Enter In The Month : "; cin >> month;
    		cout << "Enter In The Day : "; cin >> day;
    		if ( !! Validate())
    		{
    			cout << "\n" << endl;
    			Set();
    		}
    	};
    
    	void Set(int yearx, int monthx, int dayx)
    	{
    		year = yearx;
    		month = monthx;
    		day = dayx;
    		if ( !! Validate())
    		{
    			cout << "\n" << endl; 
    			Set();
    		}
    	};
    
    	void Display()
    	{
    		cout << year << ":" << month << ":" << day << endl;
    	};
    
    	bool Validate()
    	{
    		int days_in_month[] = {31, 28 + (year % 4 == 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    		if (year >= 1900)
    		{
    			if (month >= 1 && month <= 12)
    			{
    				if (day >= 1 && day <= days_in_month[month - 1])
    				{
    					return false;
    				}
    				else
    				{
     				cout << "[ERROR] Day range ( 1 to " << days_in_month[month - 1] << " )"<< endl; 
    				return true;
    				}
    			}
    			else
    			{
     				cout << "[ERROR] Month range ( 1 to 12 )" << endl; 
    				return true;
    			}
    		}
    		else
    		{
    			cout << "[ERROR] Year is to small (1900)" << endl;
    			return true;
    		}
    	};
    
    };
    
    
    
    class Employee
    {
    private:
    	int e_number, e_job, e_dateofhire; 
    	double e_salary;
    	Date dl;
    
    public:
    	void Set()
    	{
    		cout << "Enter In The Employee's Number : "; cin >> e_number;
    		cout << "Enter In The Employee's Salary : $"; cin >> e_salary;
    		cout << "[0] Manager,  [1] Laborer, [2] Secretary" << endl;
    		cout << "Enter In The Employee's Job Category : "; cin >> e_job;
    		if ( !! Validate())
    		{
    			cout << "\n" << endl; 
    			Set();
    		}
    		dl.Set();
    	}
    
    	void Set(int e_numberx, double e_salaryx, int e_jobx, Date dlx)
    	{
    		e_number = e_numberx;
    		e_salary = e_salaryx;
    		e_job = e_jobx;
    		dl = dlx;
    		if ( !! Validate())
    		{
    			cout << "\n" << endl; 
    			Set();
    		}
    		
    	};
    
    	void Display()
    	{
    		cout << "\n" << endl;
    		cout << "Employee's Number : " << e_number << endl;
    		cout << "Employee's Salary : $" << e_salary << endl;
    		cout << "Employee's Job Category : ";
    		switch (e_job)
    		{
    		case manager : cout << "Manager" << endl; break;
    		case laborer : cout << "Laborer" << endl; break;
    		case secretary : cout << "Secretary" << endl; break;
    		}
    		cout << "Date of employed "; dl.Display();
    		cout << "" << endl;
    	}
    
    	bool Validate()
    	{
    		if (e_number > 0 && e_number < 100000000)
    		{
    			if (e_salary > 0 && e_salary < 1000000)
    			{
    				if (e_job > -1 && e_job < 3)
    				{
    					return false;
    				}
    				else
    				{
    					cout << "Employee's Category 0 to 2)" << endl;
    					return true;
    				}
    			}
    			else
    			{
    			cout << "Employee's Salary 1 to 999999)" << endl;
    			return true;
    			}
    		}
    		else
    		{
    			cout << "Employee's Number 1 to 999999999)" << endl;
    			return true;
    		}
    	}
    
    };//End of Employe
    
    void main()
    {
    
    Employee dl, dl2;
    Date dx;
    
    dx.Set(2004, 2, 29);
    dl.Set(1023, 8555, 0, dx);
    dl.Display();
    
    dl2.Set();
    dl2.Display();
    
    }
    Just rename the file to Mmowned Lab1.cpp
    Attached Files Attached Files
    Last edited by DarkLinux; 10-06-2010 at 04:16 PM.

    Intro to C++ [Classes, Nesting, Casting, Enums, Low Lever Error Checking and more...]

Similar Threads

  1. [PQR] How to script (lua) Mana Sapphire for Mages (auto cast at low mana)
    By dealerx in forum WoW Bot Maps And Profiles
    Replies: 11
    Last Post: 10-11-2016, 08:34 AM
  2. The Dratini Nest in SacramentoL Low IV?
    By MithosNL in forum Pokemon GO Chat
    Replies: 10
    Last Post: 08-08-2016, 02:17 AM
  3. [All Class] My Way to go to Archerus, and more
    By Mantycore in forum World of Warcraft Exploration
    Replies: 6
    Last Post: 07-19-2010, 02:30 PM
  4. Repeat a low level quest over. and over.(horde)
    By thefun25 in forum World of Warcraft Exploits
    Replies: 8
    Last Post: 10-18-2008, 05:37 PM
All times are GMT -5. The time now is 04:07 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search