#include <iostream> #include <vector> using namespace std; int swap_values(vector<int>numbers); int main () { vector<int>numbers(3); cout << "Let's swap vector values\n"; cout << "Enter value 1 " << endl; cin >> numbers[1]; cout << "Enter value 2 " << endl; cin >> numbers[2]; cout << "vector 1: " << numbers[1] << endl; cout << "vector 2: " << numbers[2] << endl; cout << "vector 3: " << numbers[3] << endl; swap_values(numbers); cout << "vector 1: " << numbers[1] << endl; cout << "vector 2: " << numbers[2] << endl; cout << "vector 3: " << numbers[3] << endl; cin.get(); cin.get(); return 0; } int swap_values(vector<int>numbers) { numbers[3] = numbers[1]; numbers[1] = numbers[2]; numbers[2] = numbers[3]; return 1; }
#include <iostream> #include <vector> using namespace std; bool swap_values(vector<int>& numbers); int main () { vector<int> numbers(2); cout << "Let's swap vector values\n"; cout << "Enter value 1 " << endl; cin >> numbers[0]; cout << "Enter value 2 " << endl; cin >> numbers[1]; // indexing starts at 0 and not 1 so the first element is indexed by // numbers [0] and not numbers [1]. cout << "vector 1: " << numbers[0] << endl; cout << "vector 2: " << numbers[1] << endl; swap_values(numbers); cout << "vector 1: " << numbers[0] << endl; cout << "vector 2: " << numbers[1] << endl; cin.get(); cin.get(); return 0; } // btw what type of swap were u planning to implement using three values ??? // i thought u would want a swap with 2 values and changed the program likewise. bool swap_values(vector<int>& numbers) { // better make the function return bool instead of the old style "int" int temp = numbers[1] ; numbers[1] = numbers[0]; numbers[0] = numbers[1]; return true; }
1. I'm making a program with a default constructor and an overloaded constructor. They're both called by different rules and produces a testline of text when called. The assignment is to give the overloaded constructor default arguments to see what happens. I wonder, what is a default argument i an overloaded constructor?
#include <iostream>
class T
{
int a,b,c; // private data
public:
T(int a_ = 0, int b_ = 0, int c_ = 0) : a(a_), b(b_), c(c_)
{
std::cout << "default constructor with default arguments:\n";
std::cout << "a = " << a << ',';
std::cout << "b = " << b << ',';
std::cout << "c = " << c << '\n';
}
};
int main(int argc, char *argv[])
{
T t(1), u(1,2,3);
return 0;
}
/* my output
default constructor with default arguments:
a = 1,b = 0,c = 0
default constructor with default arguments:
a = 1,b = 2,c = 3
*/1. I'm making a program with a default constructor and an overloaded constructor. They're both called by different rules and produces a testline of text when called. The assignment is to give the overloaded constructor default arguments to see what happens. I wonder, what is a default argument i an overloaded constructor?
class A { int i; int j; }
A::A();
A::A(int int_i, int int_j ) { i = int_i; j = int_j; };
int_i and int_j to assign values for class A's member variables i and j.
A::A(int int_i, int int_j = 0) { i = int_i; j = int_j; };
A a(2), will create an object a with a.i = 2 and a.j = 0;A a(2,9), will create an object a with a.i = 2 and a.j = 9;2. How do you instance an object from a class in different scope?
#include <iostream>
class A
{
public:
class B
{
public:
int i;
};
};
int main()
{
A::B b;
b.i = 9;
std::cout << b.i << std::endl;
}3. How do you send an object "by value" to a function?
class A { ... } int some_function_that_takes_an_object_of_class_A( A a ) { ..... } int main() { A a; some_function_that_takes_an_object_of_class_A( a ); // sends a copy of a. the original a is not changed. }
#include <iostream>
using namespace std;
class Person
{
public:
string first_name;
string last_name;
string month;
int year;
int day;
Person();
Person(string first_name, string last_name);
~Person();
void getinfo();
void setinfo();
void deleter();
void count_instance();
private:
int *pointer;
int counter;
};
int main ()
{
Person enter;
Person pass;
Person testing();
int *pointer;
pointer = new int;
enter.setinfo();
cout << "Test: The pointer is: " << *pointer << endl;
enter.getinfo();
enter.deleter();
cout << "Test: The pointer is: " << *pointer << endl;
enter = Person(enter.first_name, enter.last_name);
cin.get();
cin.get();
enter.count_instance();
cin.get();
cin.get();
return 0;
}
void Person::setinfo()
{
counter = 0;
cout << "counter is: " << counter;
cout << "Name1?" << endl;
cin >> first_name;
cout << endl << "Name2?" << endl;
cin >> last_name;
cout << endl << "Year?" << endl;
cin >> year;
cout << endl << "Month?" << endl;
cin >> month;
cout << endl << "Day?" << endl;
cin >> day;
}
void Person::getinfo()
{
cout << first_name << " " << last_name << " " << year << " " << month << " " << day;
}
void Person::count_instance()
{
cout << "counter is: " << counter;
}
void Person::deleter()
{
delete [] pointer;
}
Person::Person()
{
counter = counter +1;
cout << "Def Constructor!" << endl;
}
Person::Person(string first_name, string last_name)
{
counter = counter +1;
cout << "Overloaded Constructor!" << endl;
}
Person::~Person()
{
cout << "DESTRUCTOR!" << endl;
} static int counter; member variable in your class. The speciality of "static" variable is that for any number of instances of the class created they all have a common copy of the instance variable. So any changes made to the instance variable using one object will be refleceted when the variable is accessed using the next object.#include <iostream>
using namespace std;
class Person
{
public:
string first_name;
string last_name;
string month;
int year;
int day;
Person();
Person(string first_name, string last_name);
~Person();
void getinfo();
void setinfo();
void deleter();
void count_instance();
static int getCount ( );
static void incrCount ( );
private:
int *pointer;
static int counter;
};
int main ()
{
Person enter;
Person pass;
Person testing();
int *pointer;
pointer = new int;
enter.setinfo();
cout << "Test: The pointer is: " << *pointer << endl;
enter.getinfo();
enter.deleter();
cout << "Test: The pointer is: " << *pointer << endl;
enter = Person(enter.first_name, enter.last_name);
cout << "\nThe object count is" << Person::getCount(); // call to static function of class
cin.get();
return 0;
}
void Person::setinfo()
{
counter = 0;
cout << "counter is: " << counter;
cout << "Name1?" << endl;
cin >> first_name;
cout << endl << "Name2?" << endl;
cin >> last_name;
cout << endl << "Year?" << endl;
cin >> year;
cout << endl << "Month?" << endl;
cin >> month;
cout << endl << "Day?" << endl;
cin >> day;
}
void Person::getinfo()
{
cout << first_name << " " << last_name << " " << year << " " << month << " " << day;
}
void Person::count_instance()
{
cout << "counter is: " << counter;
}
void Person::deleter()
{
delete [] pointer;
}
Person::Person()
{
Person::incrCount ( );
cout << "Def Constructor!" << endl;
}
Person::Person(string first_name, string last_name)
{
Person::inctCount ( );
cout << "Overloaded Constructor!" << endl;
}
Person::~Person()
{
cout << "DESTRUCTOR!" << endl;
}
static void incrCount ( )
{
Person::count ++ ;
}
static int getCount ( )
{
return Person::count ;
}
erson(std::string, std::string)': Person::Person(string first_name, string last_name)
{
Person::inctCount ( );
cout << "Overloaded Constructor!" << endl;
}Person::Person(string first_name, string last_name)
{
Person::incrCount ( );
cout << "Overloaded Constructor!" << endl;
} static void incrCount ( )
{
Person::counter ++ ;
}
static int getCount ( )
{
return Person::counter ;
}~s.o.s~ --> I tried running your modified program, but I got some errors when running it in Bloodshed Dev-C++.
This is what the log says:
In constructor `Person:erson(std::string, std::string)':
Line 89 `inctCount' is not a member of `Person'
In function `void incrCount()':
Line 101 `count' is not a member of `Person'
In function `int getCount()':
Line 106 `count' is not a member of `Person'
Any idea why it does this?
Thanks!
| DaniWeb Message | |
| Cancel Changes | |