Question

In: Civil Engineering

C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...

C++ constructor initializer list

Constructor initializer list:

        - syntax

        - when it must be used

        - how to instantiate and initialize data members that are user-defined types at the same time

Solutions

Expert Solution

Constructor initializer list:

Use : Initializer List is for initializing data members in a class.

Synatx : List of members being initialized are indicated with constructor as a comma separated list being followed by a colon. Here is an example :

#include<bits/stdc++.h>

using namespace std;   

class Test {

private:

    int a;

    int b;

public:

Test(int i = 0, int j = 0):a(i), b(j) {}

int printA() {return a;}

int printB() {return b;}    

};

int main() {

Test t(10, 15);

  cout<<"x = "<<t.printA()<<", ";

  cout<<"y = "<<t.printB();

  return 0;

}

Output of above code will be: 10 15

Must use conditions:

1. To initialize non-static constant data members.

2. To initialize reference members.

To instantiate and inititalize data that are user-defined types at the same time, that is at the time when they are being defined by the user, just see the above example of code. It is doing the same thing.


Related Solutions

Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called. Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it! // A wrong solution to this activity... PoundDog::PoundDog() { age = 1; id = -1; name.SetString("NoName");...
using correct c++ syntax, in one part write a short header file with only one constructor...
using correct c++ syntax, in one part write a short header file with only one constructor and one private variable. in another part write the function definition for the same constructor of the header file you just wrote. using a correct c++ syntax, write only the top line of a derived header file that uses a base class. write the function definitions of two constructors in this derived class
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor...
In C++ 14.22 A dynamic class - party list Complete the Party class with a constructor with parameters, a copy constructor, a destructor, and an overloaded assignment operator (=). //main.cpp #include <iostream> using namespace std; #include "Party.h" int main() { return 0; } //party.h #ifndef PARTY_H #define PARTY_H class Party { private: string location; string *attendees; int maxAttendees; int numAttendees;    public: Party(); Party(string l, int num); //Constructor Party(/*parameters*/); //Copy constructor Party& operator=(/*parameters*/); //Add destructor void addAttendee(string name); void changeAttendeeAt(string...
Create initializer/constructor functions for book. Declare and implement functions like insert book(insert at End, InsertAtFirst), retrieve...
Create initializer/constructor functions for book. Declare and implement functions like insert book(insert at End, InsertAtFirst), retrieve book, update book information book using ISBN number in Library. Declare another function named Search to find a specific book from catalogue. #include<iostream.h> #include<conio.h> struct book{ char name[50]; int ISBN; char authorname[50]; char publishername[50]; int issuedate; int issuemonth; int issueyear; int retdate; int retmonth; int retyear; struct book *next=NULL; } void insert(struct book *head){ if *head.next==NULL{ struct book node; cout<<"enter the name of book";...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must...
JavaScript - Create a class using "names" as the identifier. Create a constructor. The constructor must have elements as follow: first ( value passed will be String ) last ( value passed will be String ) age ( value passed will be Numeric ) The constructor will assign the values for the three elements and should use the "this" keyword Create a function, using "printObject" as the identifier printObject: This function will have three input parameters: allNames , sortType, message...
C++ - When working on this assignment, focus on memorizing the syntax for writing classes. Write...
C++ - When working on this assignment, focus on memorizing the syntax for writing classes. Write a simple class named Circle, with three private variables: doubles named x, y and radius. The center of the circle is denoted by coordinates (x,y), and the radius of the circle is denoted by radius. It should have public member functions with the following signatures: void setRadius(double r) void setX(double value) void setY(double value) double getRadius() double getX() double getY() double getArea() bool containsPoint(double...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .
1. From the constructor, you infer that the name of the class containing the constructor must be_______ .2. Fill in the blanks so that the statement will instantiate a JButton object and assigns it to variable of JButton type:JButton btn= _______ ("OK");3. Fill in the blanks to complete the description of the constructor in the corresponding UML class diagram:+<>______( _____ : ______)
Python: High school assignment, please keep simple In python: Use the following initializer list to create...
Python: High school assignment, please keep simple In python: Use the following initializer list to create an array: twainQuotes = ["I have never let my schooling interfere with my education.", "Get your facts first, and then you can distort them as much as you please.", "If you tell the truth, you don't have to remember anything.", "The secret of getting ahead is getting started.", "Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT