Solved

17. Can we assign default values to the parameters of methods?

Posted on 2015-01-24
1
55 Views
Last Modified: 2015-01-26
Can we assign default values to the parameters of methods?
0
Comment
Question by:Nusrat Nuriyev
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
Yes, absolutely. That would work like

class MyClass {
public:
    void foo(int x = 42) {
        std::cout << "this is 'foo()' called with " << x << std::endl;
    }
};                         

Open in new window


or

//MyClass.h
class MyClass {
public:
    void foo(int x = 42);
};

Open in new window

// MyClass.cpp
#include <iostream>
#include "MyClass.h"
    void MyClass::foo(int x) {
        std::cout << "this is 'foo()' called with " << x << std::endl;
    }

Open in new window


The default values have to be given with the declaration, not the implementation.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now