C++ :: Use A New Class On Functions?
Feb 14, 2015how use a new class on functions?
class test
{
public:
int a=0;
[Code]....
i did something like these with my image class, but i get wrong results.
how use a new class on functions?
class test
{
public:
int a=0;
[Code]....
i did something like these with my image class, but i get wrong results.
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;
[Code]...
At the moment im trying out with pointing to an array of functions. I got this working as following:
typedef void (* functionPtr) ();
functionPtr functions[2][2]={{do11,do12}, {do21,do22}};
void do11(){DEBUG_PRINTLN("11");}
void do12(){DEBUG_PRINTLN("12");}
void do21(){DEBUG_PRINTLN("21");}
void do22(){DEBUG_PRINTLN("22");}
void loop(){
A=0;
B=1;
functions[A][b]();
}
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 I have 3 classes:
class Player {
public:
virtual func1();
[code]....
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.
#include<iostream>
class A{
public:
[Code].....
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,
#include <iostream.h>
#include <conio.h>
void main();
void loop()
[Code]....
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?
I have a class as follows:
class testa {
public:
testa_b();
~testa_b();
testa_c();
}
Then I have defined some variables in testa_b:
testa::testa_b()
{
double aa = 1.0;
}
What is the easiest way to make testa_c() can use the aa defined in testa_b() (without declaring them in the class)?
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?
View 4 Replies View RelatedI 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 {
[Code] ....
Code:
#ifndef CANDIES_H
#define CANDIES_H
#include <iostream>
#include <fstream>
#include <Global.h>
class Candies
[Code]...
Code:
#include "Candies.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
[Code]...
When I tested it, the file name in getName() outputted the right file name. However the file name in loadName() does not print out anything in the console window.
I want it to be so that the strings in [I]name_addTXT.c_str() in both functions are equal.
I have a big problem with searching a solution for getting access on getters and setters of the derived classes of an interface.
Interface:
class IfParam
{
public:
IfParam();
};
Now I have a derived class for each needed datatype. E.g.
class Int32Param: public IfParam
{
public:
Int32Param();
Int32Param(IfParam* l_param);
int getValue();
void setValue(int l_value);
private:
[Code]...
My Problem now ist getting access to the getters/setters over the interface. I wish I could initialize the Params like this:
IfParam* param = new Int32Param();
param.setValue(12);
IfParam* param = new StringParam();
param.setValue("String");
But to have access to the getter/setter I have to declaire the functions in the interface as generic functions. But how? I tried to use temlates, but then i have to declaire IfParam as IfParam<int>. Thats a problem because in my original program I do not know which TypeParam the IfParam interface will be initialized with when I create the pointer.
How I can use class functions in a vector. I have 2 classes:
Gamemode class
bullets class
Inside the Gamemode class I declared: vector<bullets> Bullet and bullets * b.
If the user presses the shoot button a new bullets class will be added to the Bullet vector:
b = new bullets;
Bullet.push_back(b)
Bow I'd like to check all objects in the Bullet vector for the collision() function inside the bullets class.
I already set up a for loop which counts from 0 to the end of the vector:
for( int i=0;i<Bullet.size;i++)
{
}
My idea was to do something like:
if(Bullet[i].collision()) then erase Bullet[i]
But that doesn't work...
Im currently working on a class assignment and the pseudo code containing the instructions that I need to complete list:
//--------------------------------------------------------------------------------------------------------------
// CTOR: Point()
//
// DESCRIPTION
// Default constructor. Initializes the point to be at the origin (0, 0) and the color to black = (0, 0, 0).
//
// PSEUDOCODE
// Call Init() and pass 0, 0, 0, 0, and 0 as the parameters.
//--------------------------------------------------------------------------------------------------------------
My code for this is:
Point::Point(){
void Init(0, 0, 0, 0, 0);
}
The code I wrote for the function Init is here:
Point::Init(int mX, int mY, color mColor){
mX = 0;
mY = 0;
mColor = pInitColor;
}
My problem here is that whenever I try calling this function in the point class, I get an error next to void Init saying incomplete type is not allowed. Also visual studio is telling me that it expects a ')' after my first zero in that line.
I could do the assignment if all i had to do was create a function within one file, call it in the main class, and that would be done with it. no, my teacher wants us to have our main program, a header and a separate class file and create functions in it in which we can then use in the main file.
This is the main file:
#include <iostream>
#include "call.h"
using namespace std;
int main() {
int length; //holds the length of the call in minutes
int hour; //holds the hour od the day(0-23 military time)
[Code] .....
I've been writing my own implementation of quicksort in a .h file. I've gotten to the point where I think I'm done with the algorithm, so now I'm trying to make it so that if someone includes "quicksort.h", they will only have access to certain functions in the file. Kind of like public and private functions in a class, but without the "class" part of it. How do you do that?
View 7 Replies View RelatedSo I have an object of class NRG (Normal Rand Generator) which takes, as an argument to it's constructor, an object of class RG (Rand generator).
template<typename RG>
class NRG {
RG rg;
public:
NRG(RG r):rg(r){}
double operator()();
An object of this class will return a normal random when it's member function operator()() is called:
template<typename RG>
double class NRG::operator()() {
static int flag = 0;
static double N2 = 0.0;
if(flag==0)
[Code] ....
However, when I run this I get an error which says:
C:UsersavadhootDesktopb.cpp|69|error: 'template<class RG> class NRG' used without template parameters|
C:UsersavadhootDesktopb.cpp|69|error: expected identifier before 'operator'|
C:UsersavadhootDesktopb.cpp|69|error: two or more data types in declaration of 'operator()'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
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.
I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.
Here's my code:
Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph
[Code] ....
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
class Calc {
[Code] ....
when i built it, it showed the following errors:
1>------ Build started: Project: rough, Configuration: Debug Win32 ------
1> rough.cpp
1>e:c programs
ough
ough
ough.cpp(17): error C3872: '0xa0': this character is not allowed in an identifier
1>e:c programs
[Code] ....
Need sorting out the errors!!!
How would I add my own function to the vector class?
View 3 Replies View RelatedIs there any way to track what functions from what class are called at runtime? What I mean is a list of functions or classes which have been called at runtime.
View 1 Replies View RelatedI have a program that uses class functions to enter and print out info. The problem is with the second function answers(). Here is the whole cpp file. In the answer function I need to use an exception to exit when its the end of an array. I could just be doing it wrong. I used try/catch originally but when I used it, it caught the exception but ended the whole program.
#include <iostream>
#include <cstdlib>
#include "answering_machine.h"
using namespace std;
void AnsweringMachine::init(){
numMessages = 0;
[Code] ......
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?
We are coding a Blackjack/21 game. I have a Deck.cpp class, Deck.h, Play.cpp (holds Main), and Card.h (holds card struct). I also have a Hand class/header, but I'm not using it yet. This is what is required per instructor.I am having issues accessing the functions that are in my Deck class. I have tried a few other means to access the class's function, but I've already gotten rid of those. These three are my latest attempts with the specific errors in the comment on the line the error was happening.
ve.
Here is my Deck.h
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include "Card.h"
#include "Hand.h"
using namespace std;
class Deck
[Code]...
Suppose I make a class, something like having the constructor being invoked first makes sense, I don't have a problem with that. But, how could I limit access to functions until certain functions are called? Perhaps this isn't built into the language so you can't. And maybe this problem never comes up. For example if you have a set() and get() functions, if they are both public functions, there doesn't seem to be a way for the compiler at least now if set() never gets called you shouldn't call get(). I just see this as error prone if you need to use libraries, you have to know not to do it from documentation instead of something the compiler can check.
View 11 Replies View Relatedhow to access the private and protected member functions of the class.....
View 5 Replies View Related