• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

FBi's running C++ questions

Cool, thats not a bad effort :)

Nit picking though:
q1 - with an inheritance hierarchy you're getting the data members down to the subclasses, but the functions can also be passed down. For instance Staff and Faculty share the same functions showEmployee() and setEmployee(), you can get rid of those and simply make the declarations in Staff protected?

q2 - I had the impression that setInterestRate() should take the value you want to change it to as a parameter, not get it from the command line?

But yeah, looking good :D
 
I agree. Looking good FBi :toast:
 
thx guys .. going reason witht eh lecturer
 
ima ask him for moar work to do
 
1. Briefly discuss the differences between multilevel and multiple inheritance.
2. Write the program code that validates an email address by checking the presence of “@” symbol.
 
thanks for the hint
 
1. Briefly discuss the differences between multilevel and multiple inheritance.

For this you might want to consider the implications on storing collections of objects... Do you know what Polymorphism is?
 
Ok. Suppose this example:

Code:
Class Stationary;
Class Pen [inherits] Object;
Class Pencil [inherits] Object;
Class Paper [inherits] Object;

So the superclass is Stationary, and it has 3 subclasses Pen, Pencil and Paper.

If you create an object of type Stationary:
Code:
Stationary thisStationary;

Then Polymorphism allows you to story any item who'se dynamic type is that of Stationary or anything that inherits from it. Thus the following is perfectly legitimate:
Code:
Stationary thisStationary = new Pen();
Stationary thisStationary = new Paper();

This is particularly useful in programming - say you are modelling an office. You need a collection of all the stationary in the room but don't want to mess around with 3 seperate lists (one for pen, one for paper, one for pencil). So, you create a collection of type Stationary and you can insert into it all of the items. Note that if you do this, you can only use the items you pull out of the collection as if they were objects of the superclass. You need to down-cast the objects to their original classes.
 
Interesting Oliver_ff. I guess we have all be exposed to OOP in different ways and that shapes how we would align the objects in the heirarchy.

For instance, I would go with
Code:
Class WritingMedia
Class WritingUtelnsil

Then inherit from those base classes to produce the next level
Code:
Class Paper : WiritingMedia
Class Parchment : Writing Media
Class Pen : WritingUtensil
Class Pencil : Wirting Utensil

You could then use
Code:
Class ColoredPaperWithPen : Paper, Pen;

Maybe I'm wrong. But just a thought.
 
I have no idea how to manuplat strings

basically the user is going to input a string and then i search the string for an "@" .... if i get that i can send backa true (use a bool?) and thats what i use to determine if the input string is valid
 
Okay FBI,

I am not going to give you code. You need to learn the underlying way that this stuff works.
A string is actually an array of characters.
In the most basic sense you can get the string from the user, and then look at each character position in the string to see if it contains the "@" symbol.
You need to learn how to do this.
It is more important to learn to be able to do it by brute force than to use a method in the languange.

Something like...
Code:
private Boolean checkforgoodstring(string myString)
{
    goodstring = false;
    Foreach (char character in myString)
    {
         if character="@" then goodstring = true;
    }
    return goodstring;
}

I am used to C#, so change syntax as needed.
 
trying something
 
Depending on the language you will have to access the array manually by the index of the characters.

using syntax like myString[index];

So for instance...
Code:
String myString = "FBI";

That means that myString[0] is equal to the letter "F"
myString[1] is "B"
and myString[2] is "I"

IF the string arrays are zero based (which they usually are)
 
For loop = 0 until loop > the length of the string do something

Understanding how variables are stored in memory is really important.
Especially if you are using C++ with pointers.

Sorry FBI, gotta go. Work calls early in the morning.
I will check this thread as soon as I can to help you. :toast:
 
Code:
#include <iostream.h>
#include <conio.h>
#include <string>
using std::string;

int it;
string Input;

bool checkForAt(string);

bool checkForAt(string th)
{
     bool isit;
     
     it = th.size();
     
     for(int count = 0; count < it; count++)
     {
             if (th[count] == '@')
                isit = 1;
     }
     
     return isit;
}

int main()
{
    cout << "Enter an email address: ";
    cin >> Input;
    if(checkForAt(Input) == 1)
    {
         cout<<"This is a valid address";           
    }
    else
    {
        cout <<"This is not a valid address";
    }
    
    
    getch();
    return 1;
}


bumboclaat!
 
Whenever you want to work with Strings, the first place I always check is here:
http://www.cplusplus.com/reference/clibrary/cstring/

It lists every library function in C/C++ for manipulating strings. If you look halfway down the page you'll see this one:
strpbrk Locate character in string (function)

Well, that sounds hopeful, so click on that and read on...

Locate character in string

Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches.

The search does not include the terminating null-characters.

Parameters

str1
C string to be scanned.
str2
C string containing the characters to match.

So i'm thinking "I've got this function, that will return a pointer to a location inside a String of any character I want, provided it exists. If it doesn't exist inside my string it returns NULL."

Code:
bool checkForAt(string th)
{
    return strpbrk(th, "@");
}
 
Last edited:
Interesting Oliver_ff. I guess we have all be exposed to OOP in different ways and that shapes how we would align the objects in the heirarchy.

For instance, I would go with
Code:
Class WritingMedia
Class WritingUtelnsil

Then inherit from those base classes to produce the next level
Code:
Class Paper : WiritingMedia
Class Parchment : Writing Media
Class Pen : WritingUtensil
Class Pencil : Wirting Utensil

You could then use
Code:
Class ColoredPaperWithPen : Paper, Pen;

Maybe I'm wrong. But just a thought.

I was taught inheritance last year in my university degree using Java, which only supports single inheritance ;) It makes the hierarchy and the code a lot simpler but you have to spend a lot more time thinking about how to get your functionality down to the subclasses.
 
Last edited:
can i get a next hint?

instr("test@test.com", "@") would return 5, as the first appearance of @ is the fifth character.
Basically if instr() returns anything there is an @ you're good. Though you can use len() to determine the length of the string and make sure someone doesn't just put @ as e-mail.
 
Back
Top