Announcements | Last Post | |
---|---|---|
SoC Curricula | 09/30/2017 01:08PM | |
Demarcation or scoping of examinations and assessment | 02/13/2017 07:59AM | |
School of Computing Short Learning Programmes | 11/24/2014 08:37AM | |
Unisa contact information | 07/28/2011 01:28PM |
Is this the way to declare a class? June 15, 2006 04:34PM |
IP/Host: ---.range86-141.btcentralplus.com Registered: 18 years ago Posts: 129 Rating: 0 |
Re: Is this the way to declare a class? June 16, 2006 09:13PM |
IP/Host: 169.202.5.--- Registered: 18 years ago Posts: 19 Rating: 0 |
Re: Is this the way to declare a class? June 17, 2006 12:39AM |
IP/Host: ---.range86-141.btcentralplus.com Registered: 18 years ago Posts: 129 Rating: 0 |
Re: Is this the way to declare a class? June 17, 2006 04:56AM |
IP/Host: ---.gprs.vodacom.co.za Registered: 18 years ago Posts: 1,424 Rating: 0 |
#ifndef HEADERFILENAME_H #define HEADERFILENAME_H //includes follow below #include <string> using namespace std; //is this always required? NEVER put this in a header. Unfortunately it will take an essay to explain why. You'll have to prefix all the STL classes with "std::" to use them though class MyClass { public: //all public declarations go here //Default Constructor and overloaded constructors? like MyClass(); //and MyClass(std::string MyName); protected: //is this where i'll usually put my mutators and inspectors? like std::string GetName() const; //is the const in the right place? Yes it is (see later) void SetName(std::string MyName); private: // it would seem that class variables go here and other facilitarors? std::string MyName; //? void DisplayName(); }; //does the class implementation follow here? or is this where ADTs come in hmm, this is a tautology #endif----------- End of Quoted code ---------------------------------------
#include <iostream> #include "HeaderFilename.h" using namespace std; void main() { MyClass intance; instance.SetName("Joe Soap" cout << instance.GetName() << endl; }then you have to make both GetName() and SetName() public.
const int ConstInt = 16;You will not be allowed to change the value of ConstInt. Now, you can define ANY instance of a data type as a constant - and sometimes during passing of values in function calls, this is done automatically for you - so you can have instances of MyClass that are constants. e.g.
const MyClass instance;
The problem with this is that MyClass has member functions that alter the data members, thus defeating the point of being constant. To protect the data members, the compiler will not allow and constant to call members that alter data members. Unfortunately, you have to tell it which ones these are. That's where the const keyword at the end of the function declaration comes in. The compiler will also check to make sure that constant functions do not alter data members.
void MyFunction(int number) { // some code } // inside main() MyFunction(28 * year + 2);Here, "number" is a parametre that MyFunction expects to receive and "28 * year + 2" is the argument being passed to MyFunction to satisfy this parametre.
Re: Is this the way to declare a class? June 17, 2006 09:15AM |
IP/Host: ---.saix.net Registered: 18 years ago Posts: 1,501 Rating: 0 |
Re: Is this the way to declare a class? June 17, 2006 03:01PM |
IP/Host: ---.gprs.vodacom.co.za Registered: 18 years ago Posts: 1,424 Rating: 0 |
sum = 15 + year; sum = operator+(15, year);With this in mind, you can overload most of the operators just like you can overload ordinary functions. Like everything else in life, there are a few exceptions: there are a few that you can't overload; and operators overloads that are class members are declared differently from those that are global (outside a class, available to everything).
// Prefix increment operator int operator++(int &value) { value = value + 1; return value; } // Postfix increment operator int operator++(int &value, int) { int tempSave = value; value = value + 1; return tempSave; }
MyDate operator+(MyDate &date, int daysToAdd) { MyDate newDate = date; newDate.AddDays(daysToAdd); return newDate; }This function can be called equivalently as either of:
FinishDate = StartDate + 20; FinishDate = operator+(StartDate, 20);To do the same thing as member functions:
class MyDate { public: // other functions MyDate operator+(int daysToAdd); // rest of class definition private: int m_daysSince1AD; Total days since 1 Jan 1 AD }; MyDate MyDate::operator+(int daysToAdd) { MyDate newDate; newDate.m_daysSince1AD = m_daysSince1AD + daysToAdd; return newDate; }Once again, the following two statements are equivalent.
FinishDate = StartDate + 20; FinishDate = StartDate.operator+(20);===================================================
using namespace std; // uncovers the entire standard library using std::cout; // only uncovers coutIn the second case, if you include <iostream> you will be able to use cout without having to prefix the namespace but you'll still have to write std::endl. See Appendix D of C&D for more on namespaces.
Re: Is this the way to declare a class? June 17, 2006 06:28PM |
IP/Host: ---.range86-141.btcentralplus.com Registered: 18 years ago Posts: 129 Rating: 0 |
Re: Is this the way to declare a class? June 17, 2006 06:35PM |
IP/Host: ---.saix.net Registered: 18 years ago Posts: 1,501 Rating: 0 |
Re: Is this the way to declare a class? June 18, 2006 05:11PM |
IP/Host: ---.range86-141.btcentralplus.com Registered: 18 years ago Posts: 129 Rating: 0 |
Re: Is this the way to declare a class? June 18, 2006 05:55PM |
IP/Host: ---.range86-141.btcentralplus.com Registered: 18 years ago Posts: 129 Rating: 0 |
Re: Is this the way to declare a class? June 18, 2006 06:00PM |
IP/Host: ---.gprs.vodacom.co.za Registered: 18 years ago Posts: 1,424 Rating: 0 |
Re: Is this the way to declare a class? June 20, 2006 01:55PM |
IP/Host: ---.saix.net Registered: 18 years ago Posts: 33 Rating: 0 |
Re: Is this the way to declare a class? June 20, 2006 06:51PM |
IP/Host: ---.gprs.vodacom.co.za Registered: 18 years ago Posts: 1,424 Rating: 0 |
Date&Date::operator++ Date &Date::operator++ Date& Date::operator++ Date & Date::operator ++