I know there is a function for comparing string but i am making it to just improve my concepts.error : 2 is coming as answer when i put same sting in both place.
I'm trying to compare 2 fractions using operator overloading. The program crashes when this is called;
this is definition: bool operator == (const Fraction& f1, Fraction& f2) { if (f1==f2)return true; else return false; }
this is my calling in the main: Fraction f1, f2; cout<<"Enter in the format: 2/4 or 4/9 "; cout << "enter first fraction: "; f1.Input(); cout << "enter second fraction: "; f2.Input();
Fraction result: result = (f1 == f2);//i think problem lies here. result.Show();
and this is the prototype of the operator: friend bool operator == (const Fraction& f1, Fraction& f2);
I have trouble comparing two strings with strcmp. The bold part of the code are the parts that are not working and i hope somebody can explain to me what i did wrong. Goal of the code is to compare a city name with the name of already created cities and when two of them match to creat a Street between them. Depending on the street pointer different streets can be created
my vector containing all already created cities (it seem to work) std::vector<city*> citylist;//all cities
find city using strings and creating a road between them:
bool Map::add_street(Street *street, const string firsttown , const string secondtown) { city* hilfs1=Map::find_city(erste); city* hilfs2=Map::find_city( zweite); if (hilfs1==NULL || hilfs2==NULL) {return false ;} // Problem :both pointers are always NULL
[Code] ....
Here is also my implementation of the getname function :
I have wrote the code below that compares to strings and sees if they are equals doesn't matter the order of the words . In the question we where asked not to use any library functions like the string functions in <string.h> and we have to do that all with pointers . I debugged my code and for some reason the first loop in the function keeps looping ...
#include<stdio.h> int IsEqual(char* str1,char* str2); #define SIZE 20 void main() { char str1[SIZE]="my name is monaya",str2[SIZE]="name monaya is my"; if (IsEqual(str1,str2))
I'm writing a code generator that produces a function from the strings to the ints. I'll be using the generated code as a "from string to enum" utility. For example:
Code: enum Color { Red, Green, Blue, Banana }; // The definition of colorFromString is generated somewhere. Color colorFromString(const std::string & s);
[Code].....
The implementation of the generated code is a trie. I've seen implementations in the past (including the one at work that I'd like to replace).
Anyway, say you need to compare the region of the string
Code: const char s[] = "holiday"; from index 3 until before index 6 against the string "ida".
I can see two bits of code that my generator could produce. One is
Code: bool hasIda = std::equal(s + 3, s + 6, "ida"); and the other is Code: bool hasIda = s[3] == 'i' && s[4] == 'd' && s[5] == 'a';
The existing code generator uses the latter method, claiming (I think) that the generated instructions are more efficient on some architectures. Is there any way to determine which is better generally, or do I have to examine the assembly produced on all target platforms?
In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,
Code: class B { public: B():_y(1){} virtual ~B() { cout<<"~B()"<<endl;
[Code] ....
This sample code does exactly what I want. So does the author mean the way I did is undefined?
#include <iostream> using namespace std; class superclass; class subclass1; class subclass2;
[Code] ....
As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.
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 .
I am working on a airport reservation program and i have run into a brick wall. i want to ask the user its name, gender, passport no, age, destination, and travel class and figure out the day and flight code of the flight which i have saved in a binary file. now every thing works fine except the code and the day.
The programs important section
the flight class Code: class flights { char code[9],location[21]; public: void display(); char *retloc() //to get the Location
I have been working on an assignment where I have to add three objects of a class Matrix. The class should have the flexibility to add more than two oprands without changing any operand on Left hand side of the '=' operator.
I need an array of class objects but am unsure of how one might accomplish this. I have so far...
//element class driver code Element Arsenic(lowCeiling, highCeiling); Element Cadmium(lowCeiling, highCeiling); Element Chromium(lowCeiling, highCeiling); Element Copper(lowCeiling, highCeiling); Element Lead(lowCeiling, highCeiling); Element Nickel(lowCeiling, highCeiling); Element Molybdenum(lowCeiling, highCeiling); Element Mercury(lowCeiling, highCeiling); Element Selenium(lowCeiling, highCeiling); Element Zinc(lowCeiling, highCeiling);
What I want to do is have an admin class which will hold all the employee objects, can add them, list and calculate salaries. I'm trying to make array of objects, not sure if it's right
here is the code
Code: #include <iostream> #include <string> using namespace std;
class Employee { public: Employee(string name, short type, int salary)
For a beginners C++ lab, I have a base class Employee and two derived classes HourlyEmployee and SalaryEmployee. In main, I have a vector defined as vector <Employee *> VEmp; It's then passed to a function to get the input, which works fine. But what I'm struggling with is another function with the header "printList(const vector <Employee *> & Ve)". It's supposed to loop through the vector and call the appropriate printPay function, which is a seperate print function inside each derived class. How do I loop through the vector and print it out? I was trying to do a for loop and something like "Ve[i].printPay();", but that doesn't work. So how would I do it?
Here's some snippets of the relevant code.
class Employee { .... virtual void printPay() = 0; }; class HourlyEmployee : public Employee {
i ran the following code in the latest version of code::blocks and it tells me that the objects cout and cin are not declared in this scope. what is the problem?
I used to use Turbo C++ 3.0 and i had no problem whatsoever with that compiler. But now i am trying to move to code::blocks but it is proving very very hard as all the standards have been changed.
I am a school student and thus, we had been told to practice on Turbo C++ 3.0 and now i am unable to unlearn it. Also, if i use printf in place of cout there is no error but i want to use cout as it is what i am comfortable working with.
#include<fstream> #include<conio.h> int main() { using namespace std; char name[20];
[Code] ....
Is there some document to which i can refer so as to get the latest C++ standards which is C++0x i believe?
I am working on a project that requires me to create objects from a abstract class that has 2 child classes (that need to be derived). Any examples on how to do this? I looked online and the examples were pretty vague. the main error that I am getting is when I make a temp object with & in front of it (such as Employee &genericEmp) it throws a must be initialized error.
Running into a snag in my program. I can't seem to figure out how to have an object of a class be able to look at all the other objects of its own class.
Reasoning being, I'm working on a game with multiple ships flying around in the same space. Each ship is a class. Each ship has an x and a y, and needs to compare the angle and distance of other ships' x and y coordinates to see if they're visible on the same screen.
How to tell an object to look at objects of its own class.
Here's some code:
common.h
#ifndef COMMON_H_INCLUDED #define COMMON_H_INCLUDED int dist(int x1, int y1, int x2, int y2); int get_info(int which);
[Code] ....
Basically, I just don't know how to properly write a function, call, or how to get the info I need, to the Player::get_closest() function so that it can see the other play objects.
So i'm actually using polyphormism for calculating the area of the cross and other shapes. so how do I actually make use of dynamically create an object this way?
do I create them in my Shape2DLink or in my individual child classes?
I am trying to add matrices a and b. I am getting an error in the "add" function, probably because I have m[i] in it, and m is not an array. What is the correct way of writing the "add" member function here?
Also, although the "read" and "write" member functions of the class are working just fine, do you think there is a better way of writing them?
Code: #include <iostream> #include <cmath> #include <iomanip> using namespace std; const int rows=3; const int columns=3;