1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
deff fn [24]
4 years ago
15

Name the different types of constructors that a designer can provide in a class. Provide an example with a class showing many co

nstructors.
Computers and Technology
1 answer:
Marrrta [24]4 years ago
8 0
By default, if you do not implement a constructor, the compiler will use an empty constructor (no parameters and no code). The following code will create an instance of the MyObject class using the default constructor. The object will have the default vauesfor all the attributes since no parameters were given.

MyObject obj = new MyObject();

Another type of constructor is one with no parameters (no-arg constructor). It is similar to the default, except you actually create this constructor. The contents of the the constructor may include anything. To call a no-arg constructor, use the same line of code as above. The constructor can look like the one below:

public MyObject() {
System.out.println("This is a no-arg constructor");
}

Lastly there is the parameterized constructor. This type of constructor takes in parameters as inputs to assign to values in the newly created object. You call a parameterized constructor as follows:

MyObject obj = new MyObject("Bob", 20);

The constructor will look like this:

public MyObject(String name, int age) {
this.name = name;
this.age = age;
}

In the constructor, the keyword "this" refers to the object, so this.name is a private global variable that is being set equal to the inputted value for name, in this case "Bob".

Hope this helps!
You might be interested in
Which type of storage device is better in technology between magnetic and optical?​
Nikolay [14]

Answer:

I think the answer is optical.

5 0
3 years ago
Read 2 more answers
Write a program whose input is two integers and whose output is the two integers swapped.
Tpy6a [65]

Answer:

The program to this question can be given as:

Program:

#include <iostream>  //header file

using namespace std;   //using namespace.

void SwapValues(int* userVal1, int* userVal2); //function declaration.

void SwapValues(int* userVal1, int* userVal2) //function definition.

{ //function body.

//perform swapping

   int t = *userVal1;  

   *userVal1 = *userVal2;

   *userVal2 = t;

}

int main() //main method  

{

int n1, n2; //define variable

cout<<"Enter first number :"; //message

cin>>n1; //input by user.

cout<<"Enter second number :"; //message  

cin>>n2; //input by user.

SwapValues(&n1,&n2); //calling function.

cout<<"Swapped values"<<endl;

cout<<"first number is :"<<n1<<endl; //print value

cout<<"second number is:"<<n2<<endl; //print value

return 0;

}

Output:

Enter first number :3

Enter second number :8

Swapped values

first number is :8

second number is :3

Explanation:

The description of the above C++ language program can be given as:

  • In the program, firstly we include the header file. Then we declare and define a function that is "SwapValues()" function in the function we pass two integer variable that is "userVal1 and userVal2" inside a function, we define an integer variable that is "t" and perform swapping.  
  • Then we define the main function in the main function we define two variables that is "n1 and n2" this variable is used to take value-form user. then we pass this value to function and print the function values.

5 0
3 years ago
Question 7 of 10
SpyIntel [72]

Answer:

A

Explanation:

6 0
2 years ago
In MATLAB please.
Leona [35]

Answer:

function [Area,Perimeter,L]=f_circle(x)

 % Area: pi*x^2 area of circle with radius x

 % Perimeter: 2*pi*x perimeter length of circle with radius x

 % x can be a vector

 minargs=1;maxargs=1;narginchk(minargs,maxargs);  % only 1 input accepted

   if ~isequal('double',class(x))                   5 input has to be type double

       disp('input type not double');

       return;

   end

   Area=pi*x.^2;

   Perimeter=2*pi*x;

 end

4 0
3 years ago
"this type of file contains data that has not been converted to text"
umka2103 [35]
A <span>Binary file has not been converted to text.</span>
7 0
3 years ago
Other questions:
  • What is last mile in VOIP
    13·1 answer
  • Computer ________ involves identifying, extracting, preserving, and documenting computer evidence. a. inoculation b. forensics c
    8·1 answer
  • How many license plates are there if a license plate contains 3 letters from the 26 available in the English alphabet followed b
    15·1 answer
  • When an attacker presents a program or himself as someone else to obtain private information and pretends to be a legitimate web
    11·1 answer
  • Once the human body stops moving in a crash, the internal organs stop as well. True or false?
    11·1 answer
  • How are gems and precious metals similar?
    6·2 answers
  • Which of the following transferable skills are generally the most look for in the it <br> field
    10·1 answer
  • Anybody know any educational game sites unblocced in school besides funbrain and pb skids
    12·1 answer
  • Spam and i report
    14·2 answers
  • What is a query? State it's uses.​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!