dcsimg
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Tip of the Day
Language: C++
Expertise: Intermediate
Jun 4, 1998

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Structs as a shorthand for public classes

Traditionally, structs serve as data aggregates. However, in C++ a struct can have constructor(s), destructor and member functions just like a class. The only difference between the two is the default access type: a class has by default private access type to its members and derived objects, whereas a struct has by default public access to its members and derived objects. Therefore, structs can be used as a shorthand for classes whose members are all public rather than being confined to the traditional role of 'plain old data' containers. A good example for that is the case of abstract classes:
 
struct File { //all members are implicitly public
	virtual int Read()  = 0; 
	File(FILE *);
	virtual ~File();
};

class TextFile: File {//implicit public inheritance; File is a struct
	string path; //private member
	//...other private members 
	public:
	int Flush();
	int Read();
};

class UnicodeFile : TextFile { //implicit private inheritance
	//...
};
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date