aluhnev

//Hi all,could someone explain the make_shared(), is it used to substitute constructor?
//Here is an example
void DoAddStaff(vector<Ptr>& container)
{
    cout << "\nEnter type of employee to add ('W' for waged, 'S' for salaried): ";
    char c;
    cin >> c;
    cin.ignore();

    if( c == 'w' || c == 'W' )
    {
        cout << "\nEnter waged employee's name: ";
        string name;
        getline(cin, name);

        cout << "\nEnter waged employee's hourly rate of pay: ";
        double rate;
        cin >> rate;
        cin.ignore();

        container.push_back( make_shared<CWagedEmployee>(name, CreateEmplNum( container ), rate) );
    }

    else if( c == 's' || c == 'S' )
    {
        cout << "\nEnter salaried employee name: ";
        string name;
        getline(cin, name);

        cout << "\nEnter employee's salary: ";
        double salary;
        cin >> salary;

        container.push_back( make_shared<CSalariedEmployee>(name, CreateEmplNum( container ), salary) );
    }
    else
        cout << "Wrong type." << endl;

aluhnev

//Thank you. Can you explain why if we use standard array,like this 
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        int maxNum = numbers.Max();

        Console.WriteLine("The maximum number is {0}.", maxNum);

       // No need to use cast,but in case multidimensional array need to cast.

aluhnev

Thank you.

aluhnev

//This is my code,how do i find & print max value
namespace StartCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
           int [, ] a =  new int [4,4] {{2,3,4,5}, {34,56,25,67}, {22,44,55,77},{45,80,22,13}};

           foreach (int x in a)
               Console.WriteLine(x);

        }    
    }
}
///Thank you.

aluhnev

3 rows,4 columns ,right? its 2 dimensional ? So for 4 dim,how many commas do i need?

aluhnev

//HI All what is wrong with this code,i try to declare 4 dim. array

int [, , ,] a =  new int [3,4] = {{2,3,4,5}, {34,56,25,67}, {22,44,55,77}};
//how do i declare it an assign values  to it?Thanks.

aluhnev

//Hi all,can someone help with this problem
//I am stack,need to output all functions in main,with user input for queue and stack,but first queue
//This is my .h and .cpp of queue
#include "stdafx.h"

template <class ItemType>
struct NodeType;

template <class ItemType>
class QueType
{
public:
    QueType();
    // Class constructor.
    ~QueType();
    // Class destructor.    
    void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType newItem);
void Dequeue(ItemType& item);
void PrintQue();
private:
    NodeType<ItemType>* qFront;
    NodeType<ItemType>* qRear;

};

//template <class ItemType>
//struct NodeType
//{
//    ItemType info;
//    NodeType* next;
//};

template <class ItemType>
QueType<ItemType>::QueType()    
// Class constructor.
// Post:  qFront and qRear are set to NULL.
{
    qFront = NULL;
    qRear = NULL;
}

template <class ItemType>
QueType<ItemType>::~QueType()
// Class destructor.
{
    MakeEmpty();
}

template <class ItemType>
void QueType<ItemType>::MakeEmpty()
// Post: Queue is empty; all elements have been
// deallocated.
{
    NodeType<ItemType>* tempPtr;

    while (qFront != NULL)
    {
        tempPtr = qFront;
        qFront = qFront->next;
        delete tempPtr;
    }
    qRear = NULL;
}

template <class ItemType>
bool QueType<ItemType>::IsFull() const
// Returns true if there is no room for another
// ItemType on the free store;
// false otherwise.
{
    NodeType<ItemType>* ptr;
    ptr = new NodeType<ItemType>;
    if (ptr == NULL)
        return true;
    else
    {
        delete ptr;
        return false;
    }
}

template <class ItemType>
bool QueType<ItemType>::IsEmpty() const
// Returns true if there are no elements on the queue; false otherwise.
{
    return (qFront == NULL);
}

template <class ItemType>
void QueType<ItemType>::Enqueue(ItemType newItem)
// Adds newItem to the rear of the queue.
// Pre:Queue has been ...

aluhnev

Thank you all for help.

aluhnev

//How do i do += operator assignment in this case : ?

// .h
    CTime_24 operator+=(int seconds);
    CTime_24 operator+=( CTime_24& sTime);
//.cpp 
CTime_24& CTime_24 :: operator+=(int seconds)
{
    this->m_Seconds += seconds;
    this->m_Minutes += this->m_Seconds / 60;
    this->m_Seconds %= 60;
    this->m_Hours += this->m_Minutes / 60;
    this->m_Minutes %= 60;
    this->m_Hours %= 24;
    return *this;

}

////+= operator ///
CTime_24  CTime_24 :: operator+=(  CTime_24& sTime)
{
    CTime_24 temp;
    temp.m_Seconds = this->m_Seconds + sTime.m_Seconds;
    temp.m_Minutes = this->m_Minutes + sTime.m_Minutes + temp.m_Seconds / 60;
    temp.m_Seconds %= 60;
    temp.m_Hours = this->m_Hours + sTime.m_Hours + temp.m_Minutes / 60;
    temp.m_Minutes %= 60;
    temp.m_Hours %= 24;
    return temp;

}
///////Main/////////
////Time 4/////
    CTime_24 time4(3, 20, 45);
    time4.GetTime(hour, minute, second);
    cout << "time4  =" << hour << " : " << minute << " : " << second << endl;

    //// time 4 +=  overloaded operator call /////

    time4 = time1 + 45;
    time4.GetTime(hour, minute, second);
    cout << "time4 with += operator is  = " << hour << " : " << minute << " : " << second << endl;

    //// time4 +=  overloaded operator call /////
    //How do i call  here lets say time4 += time1;

aluhnev

//Hi all,can you please explain why using this-> in this code is optional,that is the purpose?
//Code can work with not this-> too.

CTime_24 operator+(int seconds);

CTime_24 CTime_24 :: operator+(int seconds)
{
    CTime_24 temp;
    temp.m_Seconds = this->m_Seconds + seconds;
    temp.m_Minutes = this->m_Minutes + temp.m_Seconds / 60;
    temp.m_Seconds %= 60;
    temp.m_Hours = this -> m_Hours + temp.m_Minutes / 60;
    temp.m_Minutes %= 60;
    temp.m_Hours %= 24;
    return temp;

}
 int main()

{

CTime_24 time2(6, 20, 59);
    time2.GetTime(hour, minute, second);
    cout << "time2=" << hour << " : " << minute << " : " << second << endl;

    ////OVERLOADED OPERATOR CALL///////
    time2 = time1 + 45;
    time2.GetTime(hour, minute, second);
    cout << "time2 =" << hour << " : " << minute << " : " << second << endl;

}

aluhnev

//why the getters declared twice,public,and than protected at the end of this code?
And how to assign the copy constructor between this 2 classes?
class Person
{
private:
    string name;
    string address;
    string city;
    string state;
    string zipCode;
public:
    Person() {}
    Person(const string& aName,
           const string& anAddress,
           const string& aCity,
           const string& aState,
           const string& aZipCode) : name(aName),
                                     address(anAddress),
                                     city(aCity),
                                     state(aState),
                                     zipCode(aZipCode) {}
    ~Person() {}
    // Getters
    const string& getName() const { return name; }
    const string& getAddress() const { return address; }
    const string& getCity() const { return city; }
    const string& getState() const { return state; }
    const string& getZipCode() const { return zipCode; }
    // Setters
    void setName( const string& aName );
    void setAddress( const string& anAddress );
    void setCity( const string& aCity );
    void setState( const string& aState );
    void setZipCode( const string& aZipCode );
};
class Package
{
private:
    Person sender;
    Person recipient;
public:
    Package();
    Package( const Person& aSender, const Person& aRecipient)
     : sender(aSender), recipient(aRecipient) {}
    virtual ~Package();
    virtual void CalculateCost(void) = 0;
    virtual void Display(void) = 0;
    //setters
    void setSender(const Person& aPerson);
    void setRecipient(const Person& aPerson);
    //getters
    const Person& getSender() const { return sender; }
    const Person& getRecipient() const { return recipient; }
protected:
    Person& getSender() { return sender; }
    Person& getRecipient() { return recipient; }
};

aluhnev

Should the class person be a derived class of package?

aluhnev

// In @rubberman example on line 40,41 i can not declare in my package class this 2 lines.
// sender is underlined on line 40 for me.Did i missed something?

aluhnev

//How about this version,i try to avoid copy constructor,as i never used before
//and not to sure how to apply.
#pragma once
#include<string>
using namespace std;

class Package
{
public:
    Package();
    ~Package();
protected:
    string senderName;
    string senderAddress;
    string senderCity;
    string senderState;
    string senderZipCode;
    string recipientName;
    string recipientAddress;
    string recipientCity;
    string recipientState;
    string recipientZipCode;
    double weight;
    double CostPerOunce;
public:
    //virtual void CalculateCost(double amount) = 0;
    double CalculateCost();
    virtual void Display(void) = 0;

    //setters for sender
    void SetName(string name);
    void SetSenderAddress(string address);
    void SetSenderCity(string city);
    void SetSenderState(string state);
    void SetSenderZipCode(string zipCode);

    //getters for sender
    string GetSenderName(void);
    string GetSenderAddress(void);
    string GetSenderCity(void);
    string GetSenderState(void);
    string GetSenderZipCode(void);

    //setters for recipient
    void SetrecipientName(string name);
    void SetrecipientAddress(string address);
    void SetrecipientCity(string city);
    void SetrecipientState(string state);
    void SetrecipientZipCode(string zipCode);

    //getters for recipient
    string GetRecipientName(void);
    string GetRecipientAddress(void);
    string GetRecipientCity(void);
    string GetRecipientState(void);
    string GetRecipientZipCode(void);

    //constructors

    Package(string sName, string sAddress, string sCity, string sState, string sZip, string rName, string rAddress, string rCity, string rState, string rZip, double Weight, double OunceCost);

    void SetWeight(double Weight);
    double GetWeight();
    void SetCostPerOunce(double Cost);
    double GetCostPerOunce();
};

aluhnev

Can i get a little bit more explanation on copy constractor,how they perform.As i just only guessing.I have seen some examples online,but i do not know the syntax,so hard to do it ar this stage.I learn slowly ,but move on fast.

aluhnev

Sorry for asking,but never came across it.My question is should i use copy constructor and assignment operator on this 2 classes or just in one?

aluhnev

//Hi i do the delivery company project,this are my members of the base class
//how do i create 2 constructors in base,one for sender and one for recipient?
//I did one  for sender ,but visual studio does not allow me to create identical for recipient
//Can some one  help to figure it out,please?
#pragma once
#include<string>
using namespace std;

class Package
{
public:
    Package();
    ~Package();
protected:
    string senderName;
    string senderAddress;
    string senderCity;
    string senderState;
    string senderZipCode;

    string recipientName;
    string recipientAddress;
    string recipientCity;
    string recipientState;
    string recipientZipCode;
    double weight;
    double ShippingCost;
public:
    virtual void CalculateCost(void) = 0;
    virtual void Display(void) = 0;

    //setters for sender
    void SetName(string name);
    void SetSenderAddress(string address);
    void SetSenderCity(string city);
    void SetSenderState(string state);
    void SetSenderZipCode(string zipCode);

    //getters for sender
    string GetSenderName(void);
    string GetSenderAddress(void);
    string GetSenderCity(void);
    string GetSenderState(void);
    string GetSenderZipCode(void);

    //setters for recipient
    void SetrecipientName(string name);
    void SetrecipientAddress(string address);
    void SetrecipientCity(string city);
    void SetrecipientState(string state);
    void SetrecipientZipCode(string zipCode);

    //getters for recipient
    string GetRecipientName(void);
    string GetRecipientAddress(void);
    string GetRecipientCity(void);
    string GetRecipientState(void);
    string GetRecipientZipCode(void);

    //constructors

    Package(string sName, string sAddress, string sCity, string sState, string sZip);
};

aluhnev

Thank you for explanation.

aluhnev

Thank you

aluhnev

.

aluhnev

//Hi,can someone explain the meaning  of [size -2],why not [size -1],if we narrowing the string
int palindrome(char str[], int size);

int  main()
{
 cout << "Enter a string: ";
 char str[20];

 cin.getline(str,20,'\n');
  cout << "The entered string " << ( (palindrome(str,strlen(str)+1))? "is":"is not" ) << " a Palindrome string." << endl;

 return 0;
}

int palindrome(char str[], int size)
{
 if (str[0] != str[size-2]) //if first element is not equal to last element.
  return 0;
 else if (strlen(str) <= 1)
  return 1;
 else
  return palindrome(str+1,size-2); 
  }

aluhnev

How would i express the code  for example, place(412) = 1000,so power is always 10 and number of digits in that integer 3,so 3^10 and all recursively.
Is were any way to do all in one function?And just ask user for input base.

aluhnev

//Hi,i need to create programm,which takes the user input,determins how many digits in integer
// and than raise this digit to power,what was entered by user too.
//first function noraml,second one to rais to power recursive. So if user enter number
//234,this is 3 digits,so now need recurcively 3 raise to the power .
// This  what i got so far program determins the digits from integer,but does not use power

// Power of size number.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<string>
using namespace std;
typedef long double ld;

int countDigits(int number) {
    if (number < 10) {
        return 1;
    }
    int count = 0;
    while (number > 0) {
        number /= 10;
        count++;
    }
    return count;
}

int place(ld,ld);
// testing the countDigits function
int main() {
    int num ;
    int result;
    int exp;
    cout << "enter number: "<<endl;
    cin >> num;
    cout << "Enter exponent: " << endl;
    cin >> exp;
    int numDigits = countDigits(num);
    cout << num << " has " << numDigits << " digits" << endl;
    result = place(numDigits,exp);
    cout << result;
    return 0;
}int place(ld numDigits,ld exp)
{
    if(exp >=10)
    {
     return
         numDigits*(place(numDigits,exp-1));

    }
    else
        return 1;

}

aluhnev

//Hi all,can you explain how to call this function in main.
//i ask user to enter the number,like this is it correct in main,recursevely call function?

 bool isPrime(int n, int d);
int main()
{
int number;
cout <<"Enter number >= 1";
cin >> number;
if(isPrime(number))
   {
     return 
      cout << "Yes";
      else
      return
      cout << "No";

    }
    return 0;
}
    bool isPrime(int n, int d)
    {
        if(n<2)
            return 0;
        if(d == 1)
            return true;
        else 
        {
            if(n % d == 0) 
                return false;
            else
                return isPrime(n, d - 1);
        }
    }

aluhnev

How to determine what kind of function it is : is it tail recursive ?

aluhnev

ok,i see,but still why is the answer 3?

aluhnev

int Cabin (int n);

int _tmain(int argc, _TCHAR* argv[])
{
    cout << Cabin(8) << endl;
    return 0;
}

int Cabin (int n)
{
        if (n == 1)
            return 0;
    else
            return Cabin(n/2) + 1;
}

Hi ,can someone explain why is the answer 3 here:if to folow the formula the answer is 8,5,3,2,1 and then if to return values,can not be 3,were is my mistake in logic?

aluhnev

Thank you so much for answers. I just was confused were to put T or not,like i see array can be any type,but size of array is int,so we do not tampalte it.Because as i asumed if i declare T,need to assign it to every declaration,put everywere,but looks like not.

aluhnev

//Hi all ,can you explain why does this functon has two returns?
//How to read them? Is it like if/else?
int _tmain(int argc, _TCHAR* argv[])
{
    int x, n;
    int res;
    int power(int, int);
    cout << "Enter a number:";
    cin >> x;
    cout << "Enter a power:";
    cin >> n;
    res = power(x, n);
    cout << "X^N is:" << res << endl;

    return 0;
}

  int power(int x, int n)
{

     if (n == 0)
         return 1;

     if (n % 2 == 0)
     return power(x*x, n / 2);
     return power(x*x, n / 2)*x;

}

aluhnev

Thanks for pointing to my misstakes.The whole idea is to make Locate.h with templates for the main.How will i start.I have seen examples,but then try to make in reverse(then main done ,make template)i a bit confused here.I just need to see it once in action,how to correct implement templates,as i have 2 functions.Regards