C++ :: Program To Calculate Fine Using Class - Calling Constructor
Sep 7, 2013
// Program to calculate fine using class.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class library {
long bookid;
char bookname[25];
[Code] ....
In the above program, how do I call the diff. constructors in main?
View 5 Replies
ADVERTISEMENT
Apr 15, 2013
If I have an array of some class, and that class has const members, is there some way I can call a custom constructor on elements of the array?
I can't seem to reinitialize an element in foos in the example below. A thread on stack overflow mentioned the copy constructor show allow it, but I get "no match for call to '(Foo) (Foo&)'" when I try it.
Code:
class Foo {
public:
Foo();
Foo(int x, int y);
[Code] .....
View 4 Replies
View Related
Jun 9, 2014
This program in not completed. I am creating a large program in order to calculate a company's weekly payroll. For now I am filling in the separate functions piece by piece before the rest of the program is completed.
Right now I am trying to use separate functions to call other functions. I need to ask the user for the file name and then open the file. I am no longer getting any compiler errors however when I run the program all it displays is "Welcome."
So it's not actually calling the NameTheFile function and the OpenTheFile function. It just says "Welcome" and then closes down. Why is it doing this?
/*Program to determine company's weekly payroll*/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void NametheFile() {
ifstream inputFile;
[Code] ....
View 1 Replies
View Related
Jan 25, 2014
VS 2012 / Windows 7 64 bit
class
class myclass {
public:
myclass(){/*stuff here*/}
myclass(int* p) {MyPointer = p; myclass()}
[Code] ....
it works until the myclass(int* p) calls myclass()
then the MyPointer will become CCCCCCCC (NULL value)!
is there a way to stop the second constructor from resetting the pointer value to null?
View 3 Replies
View Related
Dec 1, 2013
So i compile fine, but when i run my program, after the printf it just segfaults (core dumped)...
Code:
#include<stdio.h>#include<string.h>
#include<stdlib.h>
void main() {
unsigned int j=0, max=9;
char num[100] , str1[100], str2[max];
FILE *fp;
[Code] .....
View 4 Replies
View Related
Dec 6, 2013
I have a class that extends another class, and I want multiple constructors in the child class, but the child constructor needs to call the parent constructor. This is what I have
In the child class:
ChildClass::ChildClass() {
ChildClass(1);
}
ChildClass::ChildClass(int i)
: ParentClass(i) {
// do stuff
}
In the parent class:
ParentClass::ParentClass(int i) {
// do stuff
}
In my main program:
ChildClass child1;
// do stuff with child1
// breaks
ChildClass child2(1);
// do stuff with child2
// works fine
Using the default constructor breaks my program at runtime, but using the one with a parameter works fine. The default constructor calls the other with the same thing as the main part in the program, so I would think this should make no difference, but obviously that isn't the case.
View 3 Replies
View Related
Jan 1, 2013
Is this example correct? This example from a book
Constructor of the Base Class
Person::Person(char* n="", char* nat="U.S.A", int s=1)
{
name = n;
nationality = nat;
sex = s;
}
Constructor of the Derived Class (inherited from the base class)
Student(char* n, int s=0, char* i=""):
Person(n, s)
Why the initialized list of the base class constructor doesn't match the initialized list of the derived class constructor? I know this book is a little bit old, I'm not sure if this wrong in VC++ 2010?
View 5 Replies
View Related
Mar 1, 2013
I would like to avoid throwing things in constructors as much as possible.
Is this good design to have a static class method that checks arguments the caller will give to the constructor. The documentation of the class will say, thou shall call this method to validate thine arguments before calling the constructor, or else segfault may befall thoust.
View 14 Replies
View Related
Nov 7, 2013
I've written an Array class to create 1d,2d and 3d array and it works fine for every test : example of the constructor of the array class for 2d case:
Array::Array( int xSize, int ySize ) {
xSize_ = xSize;
ySize_ = ySize;
zSize_ = 1;
vec.resize(xSize*ySize);
}
It works fine , but when i need to use this constructor inside of other constructor, i get the "no matching function error" ,
part of my code:
class StaggeredGrid {
public:
StaggeredGrid ( int xSize1, int ySize1, real dx, real dy ) : p_ (2,2) {}
[Code] .....
View 2 Replies
View Related
Mar 19, 2015
to initialize this object? Why C++ FAQ says no? Here is my code,
Code:
class A
{
public:
A(int x, char c);
A(int x);
[code] ....
I don't have any trouble to call the constructor A(int x, char c) from another constructor A(int x).
View 10 Replies
View Related
Mar 30, 2013
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).
View 6 Replies
View Related
Mar 30, 2013
I'm having some difficulties in understanding the topic which I stated above.
View 5 Replies
View Related
Jan 6, 2015
Let's say I have a Car object , and it contains inner Engine object.
Code:
struct Car{
Engine mEngine;
};
In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:
Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}
Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?
Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};
The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
View 5 Replies
View Related
May 15, 2013
I understand it is done like this
// Calling the base class constructor
explicit CCandyBox(double lv, double wv, double hv, const char* str="Candy"): CBox(lv, wv, hv)
{
...
}
But how does the compiler know that you are initializing the base "part" of the object?
Or is this the entire reason initialization lists exist, to separate this confusion?
View 4 Replies
View Related
Feb 28, 2012
I am trying to use constructor within constructor in the same class. Is that possible. I have tried something and it shows me a error message:
error: type "mainClass" is not a direct base of "glavna"
This is the program I tried:
Code:
class mainClass {
private:
int x,y;
Code] ......
View 6 Replies
View Related
Sep 20, 2013
Suppose I have two classes A and B so how to access object of class A in constructor of B's class as a parameter.
View 6 Replies
View Related
Apr 12, 2015
I created a simple program to understand it
class TestClass {
private int x = 10;
TestClass a = new TestClass();
[Code].....
I know this is recursion but how do the compiler do this? How can it call itself when it hasnt even completed initializing every object it has? Why do VS allow this?
View 1 Replies
View Related
Apr 6, 2013
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?
View 7 Replies
View Related
May 10, 2014
class abc {
public:
int i;
abc * foooo;
};
How do you call * foooo? Say I create:
abc a;
* foooo would have some values for int i.
To get int i of *foooo, I tried a.foooo.i, which doesn't work. How do you call it?
View 5 Replies
View Related
Dec 29, 2012
I'm trying to call a function on a derived class that's in a vector of it's base class. I've made the code really simple for illustration purposes:
class Sprite {
virtual void update();
}
class Enemy : public Sprite {
virtual void update();
[Code] ....
I want to be able to just call update() on the items in the vector and the derived class update() functions be called. Currently, it always calls the Sprite update, which makes sense, but it's not what I want. Is there a way to call the derived update function without knowing the type of the derived class?
View 6 Replies
View Related
Nov 4, 2014
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.
View 1 Replies
View Related
Nov 6, 2013
Right now I have code
#include <iostream>
using namespace std;
class Rectangle {
private:
double width;
double length;
[Code] .....
it gives error ...
View 1 Replies
View Related
Feb 22, 2013
I wrote code for printing a double triangle. Its woking fine upto input is 7. but when the input is 8 the output is disturbed. the code is this.
#include<iostream>
using namespace std;
int main() {
int a;
cout<<"Enter a number"<<endl;
[Code] ....
View 3 Replies
View Related
Apr 10, 2013
Whenever I try to call merge sort on large numbers say n=10000000. It gives an error. It works fine for small numbers, even though I have declared my Lists on the heap.
The below code is just the divde part.
List<long>* Merge(List<long> *ParentList) {
if((ParentList)->Length==1) {
return ParentList;
[Code] .....
View 1 Replies
View Related
May 29, 2013
I really confused with constructor (default constructor and constructor with parameters)
I coded this problem
and I worked almost, but I stock in constructor
Code:
class Tier {
public:
enum TIER_MAKE
[Code] ....
This is tier class and I have to finish constructor in class car (for simple, I skip detail code) -red things are the parts from class Tier
Code: Car()
: make(NULL), passengers(0), fuelcap(0.0), efficiency(0.0), tier(Tier::nexen)
{ }
[Code] ....
And someone said default constructor part has to be this
Code:
car( Tier::TIER_MAKE p_tiermaker = Tier::nexen )
//after i skip
but default constructor should be no parameter...? isn't it?
View 1 Replies
View Related
Mar 22, 2014
# include <iostream>
# include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
class Course
// Creating the class Course
[Code] ....
Errors: Warning1warning C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
[Code] .....
I have to create an Array of type Course and then fill its member dats using various member functions. Those errors are caused by some Constructor defect, which I dont really know what it is.
View 2 Replies
View Related