I have Test for C++, Can you give me a example of Class design in C++ Programmin
ID: 3694816 • Letter: I
Question
I have Test for C++, Can you give me a example of Class design in C++ Programming? I have a list of Class design. Also, can you use <iostream> please?
THANK you
Class design a. Accessibility i. Private vs. public ii. Scope resolution operator: (used when defining member functions/methods outside of the class definition) b. Accessor methods Typically return one private data member i. ii. c. Mutator methods i. const keyword to protect the method from mutating/changing the object Typically void methods (return nothing) Default constructor d Constructor i. I. 2. No input arguments C++ provides a default constructor, but we typically define our own to have control of how the object is constructed. ii. Parameterized constructor 2. Used to allow parametric construction of the object, i.e. you can choose the values of the data members right as you create the object iii. Constructor call syntax: 1. Classname objectName (paraml, param2, etc.);//parameterized 2. Classname objectName // default e. Free-standing functions (that don't belong to or are members of the class) i. ii. We can pass class objects to a function Almost always pass class objects by reference. This is more efficient than call by value. 1. void myFreestandingFunction (Classname &objectName;);Explanation / Answer
class Base {
public:
// public members go here
protected:
// protected members go here
private:
// private members go here
};
class PublicAccess
{
public: // public access specifier
int x; // Data Member Declaration
void display(); // Member Function decaration
}
Private
Private keyword, means that no one can access the class members declared private outside that class. If someone tries to access the private member, they will get a compile time error. By default class variables and member functions are private.
class PrivateAccess
{
private: // private access specifier
int x; // Data Member Declaration
void display(); // Member Function decaration
}
Mutators;
class MaClasse{
private :
TypeDeMaVariable MaVariable;
public :
void SetMaVariable(TypeDeMaVariable);
};
MaClasse::SetMaVariable(TypeDeMaVariable MaValeur){
MaVariable = MaValeur;
}
In the above example, the mutator of data member could be the following:
class Toto{
private :
int _age;
public :
void SetAge(int);
};
void Toto::SetAge(int age){
_age = age;
}
Constructors
A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.
Default Constructors
Following example explains the concept of constructor:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor
private:
double length;
};
// Member functions definitions including constructor
Line::Line(void)
{
cout << "Object is being created" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Object is being created
Length of line : 6
Following example explains the concept of constructor:
Parameterized Constructor:
A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation as shown in the following example:
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(double len); // This is the constructor
private:
double length;
};
// Member functions definitions including constructor
Line::Line( double len)
{
cout << "Object is being created, length = " << len << endl;
length = len;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
// Main function for the program
int main( )
{
Line line(10.0);
// get initially set length.
cout << "Length of line : " << line.getLength() <<endl;
// set line length again
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
When the above code is compiled and executed, it produces the following result:
Object is being created, length = 10
Length of line : 10
Length of line : 6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.