- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements