
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Passing a vector to constructor in C++
This is a simple C++ program to pass a vector to a constructor.
Algorithm
Begin Declare a class named as vector. Declare vec of vector type. Declare a constructor of vector class. Pass a vector object v as a parameter to the constructor. Initialize vec = v. Declare a function show() to display the values of vector. for (int i = 0; i < vec.size(); i++) print the all values of variable i. Declare v of vector type. Initialize some values into v in array pattern. Declare ob as an object against the vector class. Pass values of v vector via ob vector object to class vector. Call show() function using vector object to show the all values of vector v. End.
Example Code
#include <iostream> #include <vector> using namespace std; class Vector { vector<int> vec; public: Vector(vector<int> v) { vec = v; } void show() { for (int i = 0; i < vec.size(); i++) cout << vec[i] << " "; } }; int main() { vector<int> v = {7,6,5,4}; Vector ob(v); ob.show(); return 0; }
Output
7 6 5 4
- Related Articles
- How to append a vector in a vector in C++?
- How to call the constructor of a superclass from a constructor in java?
- How to declare a constructor in Java?
- Passing Arguments to a Subroutine in Perl
- How to call a static constructor or when static constructor is called in C#?
- How to initialize a const field in constructor?
- How to create a parameterized constructor in Java?
- How to create a default constructor in Java?
- Passing empty parameter to a method in JavaScript
- How to convert a string vector into an integer vector in R?
- Reference to a constructor using method references in Java8
- How to generate a normal random vector using the mean of a vector in R?
- How to multiply each element of a larger vector with a smaller vector in R?
- Add a property to a JavaScript object constructor?
- Add a method to a JavaScript object constructor?

Advertisements