SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    736
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question C++ - Functions are Public/Private by default ?

    Hi

    I am just confused, when you declare a function in a class, is it public or private by default?


    Pls help

  2. #2
    Non-Member thewebhostingdir's Avatar
    Join Date
    Oct 2005
    Posts
    703
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By default, all declarations are private.

  3. #3
    SitePoint Member
    Join Date
    Jul 2009
    Location
    Cyberspace
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You really should make it clear if it is public, private or protected in your code by adding the correct statements. But yeah private unless stated otherwise.

    There is a good guide to classes at cplusplus.com/doc/tutorial/classes/
    Toolbar Buttons - buttons for everying
    Text Formatting Toolbar - make posting easy and fun
    MinimizeToTray Plus

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Hi

    I am just confused, when you declare a function in a class, is it public or private by default?


    Pls help
    Well let's see:
    Code Cpp:
    #include <iostream>
    using namespace std;
     
    class Apple
    {
    	void sayhi()
    	{
    		cout<<"hi"<<endl;
    	}
    };
     
    int main()
    {
    	Apple a;
    	a.sayhi();
     
    	return 0;
    }
     
    --output:--
    error: 'void Apple::sayhi()' is private


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •