Results 1 to 3 of 3
Thread: C++
Hybrid View
-
10th May 2012 04:25 #1
C++
,
:
, .
:
Code:#include <iostream> using namespace std; // class Quadratic_int definition class Quadratic_int{ public: Quadratic_int(); ~Quadratic_int(); Quadratic_int& operator=(const Quadratic_int& q); Quadratic_int(const Quadratic_int& q); int get_a() const; int get_b() const; int get_c() const; private: int* a; }; // Constructor for class Quadratic_int (default coeficients = 3) Quadratic_int::Quadratic_int (){ const int SIZE = 3; // number of default coeficients a = new int[SIZE]; // Dynamically alocating an array for (int i = 0; i< SIZE; i++) // A loop to get value for each member of the array, included check for correctly entered values { bool check = false; do { cout<<endl<<"Please enter a coeficient "<<i+1<<" "; cin>> a[i]; if(cin.fail()) { check = true; cin.clear(); cin.ignore(1,'\n'); } else { check = false; } }while(check); } } Quadratic_int::~Quadratic_int(){ delete[] a; } Quadratic_int& Quadratic_int::operator=(const Quadratic_int& q){ if(this != &q) { delete[] a; if(q.a == NULL) {a = NULL;} else { const int SIZE = 3; a = new int[SIZE]; for(int i = 0; i < SIZE; i++){ a[i] = q.a[i]; } } } return *this; } Quadratic_int::Quadratic_int(const Quadratic_int& q){ if(q.a == NULL) { a = NULL; } else { const int SIZE = 3; a = new int[SIZE]; for(int i = 0; i < SIZE; i++){ a[i] = q.a[i]; } } } int Quadratic_int::get_a() const{ return a[0]; } int Quadratic_int::get_b() const{ return a[1]; } int Quadratic_int::get_c() const{ return a[2]; } int main() { Quadratic_int m; Quadratic_int s(m); cout<<"m - "<<m.get_a()<<"x^2 + "<<m.get_b()<<"x + "<<m.get_c()<<endl; cout<<"s - "<<s.get_a()<<"x^2 + "<<s.get_b()<<"x + "<<s.get_c()<<endl; Quadratic_int t; Quadratic_int r; r = t; cout<<"r - "<<r.get_a()<<"x^2 + "<<r.get_b()<<"x + "<<r.get_c()<<endl; cout<<"t - "<<t.get_a()<<"x^2 + "<<t.get_b()<<"x + "<<t.get_c()<<endl; system("PAUSE"); return 0; }

- = (.. Quadratic_int s = m; ) ? = ? , - (s.) s (dangling pointer) ? delete[] a; ?
, .
.. DevC++ v4.9.9.2.
.2.
-
10th May 2012 09:22 #2
. , T:
copy constructor- .Code:T a; T b(a);
copy constructor-.Code:T a; T b = a;
operator =.Code:a; b; b = a;
- 'b' - copy-constructor-, ( ). dangling pointer.
- a == NULL, , , .
. , ((), ~T(), T(const T& rhs), T& operator = (const T& rhs)). - - _destroy() _copy(), ~T() _destroy(), (const T&) _copy(rhs), a operator= this == &rhs , _destroy() _copy(). , .
2. , ., . .
"640K ught to be enough for anybody" - Bill Gates, 1981
::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel
-
10th May 2012 22:59 #3
. - ,

--------- 22:59 --------- : 18:04 ---------
, . , /
. main .
Code:#include <iostream> using namespace std; // class Quadratic_int definition class Quadratic_int{ public: Quadratic_int(); ~Quadratic_int(); Quadratic_int& operator=(const Quadratic_int& q); Quadratic_int(const Quadratic_int& q); int get_a() const; int get_b() const; int get_c() const; void set_a(int x); void set_b(int x); void set_c(int x); void calc_funct(); private: void _copy(const Quadratic_int& b); int* coefficients; }; // Constructor for class Quadratic_int (default coeficients = 3) Quadratic_int::Quadratic_int (){ cout<<"Call to the Quadratic_int default constructor"<<endl; // Used to demonstrate when there is call to the function const int SIZE = 3; // number of default coeficients coefficients = new int[SIZE]; // Dynamically alocating an array for (int i = 0; i< SIZE; i++) // A loop to get value for each member of the array, included check for correctly entered values { bool check = false; do { cout<<endl<<"Please enter a coeficient "<<i+1<<" "; cin>> coefficients[i]; if(cin.fail()) { check = true; cin.clear(); cin.ignore(1,'\n'); } else { check = false; } }while(check); } } // Destructor for the Quadratic_int class Quadratic_int::~Quadratic_int(){ cout<<"Call to the Quadratic_int destructor"<<endl; // Used to demonstrate when there is call to the function delete[] coefficients; } // Operator = overloading for the Quadratic_int class Quadratic_int& Quadratic_int::operator=(const Quadratic_int& q){ cout<<"Call to the overloaded for Quadratic_int = operator"<<endl; // Used to demonstrate when there is call to the function if(this != &q) { delete[] coefficients; _copy(q); } return *this; } // Copy constructor for the Quadratic_int class Quadratic_int::Quadratic_int(const Quadratic_int& q){ cout<<"Call to the Quadratic_int copy constructor"<<endl; _copy(q); } // Member function used to get value of coefficient a represented in the array by coefficients[0] int Quadratic_int::get_a() const{ return coefficients[0]; } // Member function used to get value of coefficient b represented in the array by coefficients[1] int Quadratic_int::get_b() const{ return coefficients[1]; } // Member function used to get value of coefficient c represented in the array by coefficients[2] int Quadratic_int::get_c() const{ return coefficients[2]; } // Member function used to set the value of coefficient a represented in the array by coefficients[0] void Quadratic_int::set_a(int x){ coefficients[0] = x; } // Member function used to set the value of coefficient b represented in the array by coefficients[1] void Quadratic_int::set_b(int x){ coefficients[1] = x; } // Member function used to set the value of coefficient c represented in the array by coefficients[2] void Quadratic_int::set_c(int x){ coefficients[2] = x; } // Member function used to calculate the quadratic function for argument @arg manually entered by the user void Quadratic_int::calc_funct(){ cout<<endl<<"Please enter a value for the argument: "; double arg; cin>> arg; double temp = coefficients[0]*arg*arg + coefficients[1]*arg + coefficients[2]; cout<<endl<<"The value of the quadratic polynomial is "<<temp<<endl; } // Helper member function used to create new array and fill it with the values of the array of the passed argument void Quadratic_int::_copy(const Quadratic_int& b){ const int SIZE = 3; coefficients = new int[SIZE]; for(int i = 0; i < SIZE; i++){ coefficients[i] = b.coefficients[i]; } } // Operator + overloading for two Quadratic_int functions Quadratic_int operator+(Quadratic_int q1, Quadratic_int q2){ cout<<"Call to the overloaded for Quadratic_int + operator "<<endl; // Used to demonstrate when there is call to the function Quadratic_int m = q1; m.set_a(m.get_a() + q2.get_a()); m.set_b(m.get_b() + q2.get_b()); m.set_c(m.get_c() + q2.get_c()); return m; } // Operator - overloading for two Quadratic_int functions Quadratic_int operator-(Quadratic_int q1, Quadratic_int q2){ cout<<"Call to the overloaded for Quadratic_int - operator "<<endl; // Used to demonstrate when there is call to the function Quadratic_int m = q1; m.set_a(m.get_a() - q2.get_a()); m.set_b(m.get_b() - q2.get_b()); m.set_c(m.get_c() - q2.get_c()); return m; } // Unary operator * overloading for Quadratic_int // Used to check if the function have real roots bool operator*(Quadratic_int q){ bool check; int a = q.get_a(); int b = q.get_b(); int c = q.get_c(); if(b*b-4*a*c < 0 ){ check = false; } else { check = true; } return check; } // Operator << overloading in order to print the function with coefficients contained in the passed argument ostream& operator<<(ostream& out, Quadratic_int q){ cout<<q.get_a()<<"x^2"; if (q.get_b()>=0){ cout<<" +"<<q.get_b()<<"x"; } else { cout<<q.get_b()<<"x"; } if (q.get_c()>=0){ cout<<" +"<<q.get_c(); } else { cout<<q.get_c(); } return out; } // Operator >> overloading in order to change the coefficients contained in the array of the passed argument istream& operator>>(istream& in, Quadratic_int& q){ cout<<endl<<"Enter value for coefficient a: "; int a; cin>>a; q.set_a(a); cout<<endl<<"Enter value for coefficient a: "; int b; cin>>b; q.set_b(b); cout<<endl<<"Enter value for coefficient a: "; int c; cin>>c; q.set_c(c); return in; } int main() { Quadratic_int m; Quadratic_int s = m; cout<<m<<endl; cout<<s<<endl; Quadratic_int t; Quadratic_int r; r = t; cout<<r<<endl; cout<<t<<endl; t.calc_funct(); cin>>r; s = m+t+m; cout<<s<<endl; s = t-m; cout<<s<<endl; if(*s){cout<<"This function has real roots"<<endl;} else {cout<<"This function doesn't have real roots"<<endl;} system("PAUSE"); return 0; }
.. @anrieff




Reply With Quote

Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in