C++ :: Optional / Default Parameters In Class Functions?
Aug 19, 2014
In the thread "Making a argument optional to function", it is stated that to set default values for arguments of a function you can simply do so in the function definition, like:
int myfunc(int a, int b, int c=3) {...}
This then automatically puts c to 3 in the function body if a call like myfunc(1,2); is made, if I understood correctly. However, this does not seem to hold for class functions. For example, something like:
class classy {
public:
int class_func(int, int, int); // class function prototype
}
int classy::class_func(int a, int b, int c=3) {...}
fails to compile. What I would like is to be able to call class_func outside of this class (by including it as a header in another macro), optionally specifying c. If c is not specified in the call, it should use a default value.
Here is the assignment: (3pts) Given the following class header file, write the class’ source code for each of the accessor and mutator functions listed. (How the functions have listed their parameters, varying between passing by reference and by value.) Don’t forget to comment your code – it counts!
class Album { private: char * artist; // band or singer’s name char * title; // title of the album
[code]....
The input will be an array. My questions: First, am I on the right track?
When using (char * a) for a function, for example, this is passing the address of a, correct? so then *artist=a; changes what the address of a points to?
also, the functions are bool when I would expect void. Why? for all of the set_" " functions, the parameter is *... but for set_record_label it is *&. That appears to be a mistake to me. Is that right?
what is the difference between *& and * as parameters?
I have developed an application in C++ that creates some text files in a directory chosen by the user.
How can I ask the user set a Default Directory Path (and some other default parameters) so that she doesn't have to enter the same data in the GUI everytime the application is run.
The application has been developed using Qt Creator.
void f(std::vector<int> const &v, std::vector<int>::const_iterator it = v.end()) { } int main() { f({}); } prog.cpp:4:73: error: local variable ‘v’ may not appear in this context void f(std::vector<int> const &v, std::vector<int>::const_iterator it = v.end())
Why is this not allowed? (I mean, what is the reasoning for defining the standard this way?)
In C++14/C++17 we will have a unified way to represent end iterators without an instance of the container, but currently I just have to hope my implementation accepts a default-constructed iterator as an end iterator.
I wrote a fuction in C with the prototype 'void raisePowerOf2(int array[],int pow);'
If someone want to find the value of 2^456 ,just have to invoke this function 456 as the value for pow and int array with 2 elements :1 & -1 as the argument for the array.(There I used -1 to denote the end of the array.)
But it seems that this function doesn't give the exact answer
And I tried this from java also,with the same implementation.It produced the answer precisely .
I tried for hours, but unable to detect reasons why this code blok in C doesn't work properly
This is the code in c
Code:
#include<stdio.h> void raisePowerOf2(int array[],int pow); int main(){ int a[2]={1,-1}; raisePowerOf2(a,5); return 0; } void raisePowerOf2(int array[],int pow){
[Code]...
This is the code in java....
Code:
public class NewClass4 { void raisePowerOf2(int array[],int pow){ final int len=array.length; int store[]=new int[len+1]; int qtnt=0; for(int i=len-1;i>=0;i--){ store[i+1]=(array[i]*2)%10+qtnt; qtnt=(array[i]*2)/10;
If someone want to find the value of 2^456 ,just have to invoke this function 456 as the value for pow and int array with 2 elements :1 & -1 as the argument for the array.(There I used -1 to denote the end of the array.)
But it seems that this function doesn't give the exact answer
And I tried this from java also,with the same implementation.It produced the answer precisely .
I tried for hours, but unable to detect reasons why this code blok in C doesn't work properly .
This is the code in c
#include<stdio.h> void raisePowerOf2(int array[],int pow); int main(){ int a[2]={1,-1}; raisePowerOf2(a,5); return 0; } void raisePowerOf2(int array[],int pow){
// Music Shuffle Program // This program takes an array of strings and randomly permutes their order. // This allows us to generate new song shuffles. #include <iostream>
I'm trying to write a simple Delegate class with a Bind() and Invoke() function. For now it only needs to support a void class function with no parameters. I've searched around and found quite a few exmaples, though, those class are heavily templated and I lose track trying to simplify it.
So far my code is following:
Code: #include <windows.h> class Test { public: void DoSomething() { MessageBox(NULL, L"Test::DoSomething!", NULL, 0);
[Code] ....
The part I am having difficulty with is assigning &Test::DoSomething to the m_Callback variable.
&tObject::DoSomething works, yet _Callback which I pass &Test::DoSomething to does not work.
Summary: 6 companies have a product in 5 different warehouses. Each company is identified by a positive ID number and each warehouse is identified by a number (1 for the first, 2 for the second,…)
Object: the object of this assignment is to write a C++ program which asks the user to enter a company ID number, and the number of products in each of the warehouses. It then computes and prints the total number of products for that company
If the total number of product is less than 100, it prints the message “place a new order”
Input: for each company, its ID, and the number of products in each warehouse with appropriate prompt messages. Example: Enter company ID number: 101 Enter number of products in warehouse #1:30 Enter number of products in warehouse #2:60 Enter number of products in warehouse #3:0 Enter number of products in warehouse #4:5 Enter number of products in warehouse #5:27 The total for company 101 is:122
Output: for each company, the total number of product, and the message “place a new order” if the total number of product is less than 100.
Method: 1. Define global variable, int total_prod to hold the total number of products for a company 2. define the function void compute_total() that uses a loop to read the number of the products in all warehouses for one company, computer the total number of products and store it into the global variable total_prod. 3. Define the function void new_order() that determines if a new order need to be placed as follows: if the total number of products (in the global variable total_prod) is less than 100, it prints the message “place a new order” 4. Your function main does the following in a loop: - read a company ID number - call function compute_total() to read the number of the product in all warehouses for that company, and to compute their sum - print the total number of the product for that company with an appropriate message - call the function new_order() to determine if a new order need to be placed.
The reason that class members are private by default is because, in general, an object of a class should be a self-contained entity such that the data that make the object what it is should be encapsulated and only changed under controlled circumstances. Public data members should be very much the exception. As you’ll see a little later in the chapter, though, it’s also possible to place other restrictions on the accessibility of members of a class.
(I'm not sure why I used an intermediate string; it's pretty much legacy-code at this point, which I just reuse every time. Still works, so why change it!)
The problem is I'm using two types of data sets now, and the difference is one (optional). Most data files just have an arbitrarily large number if the second must be ignored, but others have nothing.
In the normal case, I'd simply use sline >> d >> L; to extract the parameter values. However, I'm not sure how this line will behave if the second parameter is omitted. Will it read nonsense? How do I check whether or not the parameter was set or not?
I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.
We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.
As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.
NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.
Stack.h
#include "stackADT.h" #include "unorderedLinkedList.h" template<class Type> class Stack: public stackADT<Type>{ template <class T> struct nodeType { T info; nodeType<T> *link;
class Date Date(int=1, int=1, int=1990); class Person Person(string="", string="", Date=NULL); class RealEstateAgent:Public Person RealEstateAgent(string="",string="",Date=NULL,Date=NULL,int=NULL, double=0.0); }
[code]....
how can I assign default values with Customer object and RealEstateAgent?
But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:
error: argument of type 'void (Basic::)()' does not match 'void (*)()'
Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:
//function fight() fight(Player p1, Player p2) { p1.func2(); } //using function fight() fight(human, computer);
When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
I would like to define a templated class while implementing default value on templated arguments. I don't know how to do that with string templated variables.
For exemple:
Code: template <class T> class A { public: A() { version = ???? } std::string_base<T> version; };
I don't want to pass the default value as parameter of the constructor. how I can do this?
Just a few moments ago i was just doing foolish things in c++ and discovered something new. Though some of you might have known this, for those who dont know, take a look at the follwing 2 small programs,
so here is my problem. i think u wud have figured out what m trying to do above. am actually calling the main() of the class and from there, i want to call the usual main... the problem is, during A.main()'s run, if i refer to main(); , that call represents itself, that it, it is like it calls itself. How on earth can i call the outside main?
If you are doing some big program, usually, how do you organize the files? Put the class and its member in head file, but where to declare non member functions and where to define them? I don't want to put them all in one cpp file. If not, how to make them visible to the main cpp file?
I want to design a class that will open a text file and manipulate the data in it. And I have to use all these functions from fstream like ifstream, ofstream, seekg etc. The problem is that I can't get the first part to work (getting my constructor to open the file using ifstream). I've posted my test.h file below.
#ifndef TEST_H_INCLUDED #define TEST_H_INCLUDED #include <iostream> #include <fstream> #include <string> using namespace std; using std::ifstream; class Test {