Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2015-01-24
1
60 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
ID: 40568218
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need some help with listview limit text 20 109
object oriented programming on screen browser tutorial lesson 2 82
How can i compile this github project?? 2 88
max float value 3 51
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

791 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